I am trying to use XSL to convert XML attributes from having values of "true"/"false" to values of "1"/"0", but I am unable to make the conversion. From reading online, I should be able to use either <xsl:when> or <xsl:if> to accomplish this. Some references wrap my attribute in string, some do not.
Take the example XML:
<root>
<record name="a" isMutable="true">
<record name="b" isMutable="false">
</root>
And the example XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet>
<xsl:template match="root">
<top>
<xsl:apply-templates select="/root/record"/>
</top>
</xsl:template>
<xsl:template match="/root/record">
<xsl:element name="element">
<xsl:choose>
<xsl:when test="string(@isMutable) = 'true'">
<xsl:attribute name="when_value">1</xsl:attribute>
</xsl:when>
<xsl:when test="string(@isMutable) = 'false'">
<xsl:attribute name="when_value">0</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="plain_value"><xsl:value-of select="@isMutable"/></xsl:attribute>
<xsl:attribute name="bool_value"><xsl:value-of select="boolean(@isMutable = 'true')"/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
My hope is to get attributes with when_value="0" and when_value="1", instead I get:
<top>
<element plain_value="true" bool_value=""/>
<element plain_value="false" bool_value=""/>
</top>
I can see in the plain_value attribute that my attribute value is getting picked up. I also tried an alternate solution which tries to use boolean, but you can see that this produces a blank result. Can anyone please point out my syntax error(s) in my example XSL code? Thank you.
No comments:
Post a Comment