Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<xsl:variable> Print out value of XSL variable using <xsl:value-of>

I'm trying to output a variable's literal string value, after it is being set depending on whether a node exists or not. I think the condition check logic is correct. But it is not outputing the values...

<xsl:variable name="subexists"/>

<xsl:template match="class">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
<xsl:choose>
    <xsl:when test="joined-subclass">
        <xsl:variable name="subexists" select="'true'"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="subexists" select="'false'"/>
    </xsl:otherwise>
</xsl:choose>
subexists:  <xsl:value-of select="$subexists" />

I want it to output the literal string of either "true" of "false". But it is not outputing anything. Please help! Thank you!!!

like image 674
tomato Avatar asked Apr 13 '09 00:04

tomato


People also ask

How do I change the value of a variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.


1 Answers

In this case no conditionals are needed to set the variable.

This one-liner XPath expression:

boolean(joined-subclass)

is true() only when the child of the current node, named joined-subclass exists and it is false() otherwise.

The complete stylesheet is:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:template match="class">
   <xsl:variable name="subexists"
        select="boolean(joined-subclass)"
   />

   subexists:  <xsl:text/>
   <xsl:value-of select="$subexists" />
 </xsl:template>
</xsl:stylesheet>

Do note, that the use of the XPath function boolean() in this expression is to convert a node (or its absense) to one of the boolean values true() or false().

like image 138
Dimitre Novatchev Avatar answered Sep 23 '22 02:09

Dimitre Novatchev