在根据节点是否存在设置变量后,我尝试输出变量的文字字符串值。我认为条件检查逻辑是正确的。但它没有输出值...
<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" />
我希望它输出“true”或“false”的文字字符串。但它没有输出任何东西。请帮忙!谢谢!!!
最佳答案
在这种情况下,不需要条件来设置变量。
这个单行 XPath 表达式:
boolean(joined-subclass)
仅当名为joined-subclass
的当前节点的子节点存在且存在时才为true()
否则为 false()
。
完整的样式表是:
<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>
请注意,XPath 函数的使用 boolean()
此表达式中的 是将节点(或其缺席)转换为 bool 值 true()
或 false() 之一
。
关于xslt - <xsl :variable> Print out value of XSL variable using <xsl:value-of>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/742676/