0

I have a XML like:

<all>
    <one>Something 1</one>
    <two>something 2</two>
    <check>
        <present>true</present>
    </check>
    <action>
        <perform></perform>
    </action>
</all>

I want to perform XML Transformation using XSL:

expected output: if <present>true</present>

all>
    <one>Something 1</one>
    <two>something 2</two>
    <check>
        <present>YES</present>
    </check>
    <action>
        <perform>READ</perform>
    </action>
</all>

else if : <present>false</present>

<all>
    <one>Something 1</one>
    <two>something 2</two>
    <check>
        <present>NO</present>
    </check>
    <action>
        <perform>INSERT</perform>
    </action>
</all>

Is it possible to Do? I am not aware about condition checking in XSL I try to move the element but did not worked:

  <xsl:template match="perform">
  <xsl:copy>
    <xsl:choose>
      <xsl:when test="../check/present = 'true'">
        <xsl:text>READ</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

2 Answers 2

1

Why don't you do exactly what you say needs to be done:

<xsl:template match="perform">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="../../check/present='true'">READ</xsl:when>
            <xsl:when test="../../check/present='false'">INSERT</xsl:when>
        </xsl:choose>
    </xsl:copy>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<xsl:stylesheet version= "1.0"
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= "xml" version= "1.0" encoding= "UTF-8" indent= "yes" />
<xsl:strip-space elements= "*" />

<!-- identity transform -->
<xsl:template match= "@*|node()" >
    <xsl:copy><xsl:apply-templates select= "@*|node()" /></xsl:copy>
</xsl:template >

<xsl:template match="action"/>

<xsl:template match="check/present">
     <xsl:choose>
          <xsl:when test=".='true'">
               <xsl:copy><xsl:text>YES</xsl:text></xsl:copy>
               <action>
                    <perform><xsl:text>READ</xsl:text></perform>
              </action>
          </xsl:when>
          <xsl:otherwise>
               <xsl:copy><xsl:text>NO</xsl:text></xsl:copy>
               <action>
                    <perform><xsl:text>INSERT</xsl:text></perform>
              </action>
          </xsl:otherwise>
     </xsl:choose>
</xsl:template>
</xsl:stylesheet>

1 Comment

I have one more concern. Just wanted to know is it possible to put the xsl file in git repo and read its contends on local and do the transformation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.