Reputation:
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>
Upvotes: 0
Views: 73
Reputation: 1276
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>
Upvotes: 0
Reputation: 117102
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>
Upvotes: 1