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

标签 xslt xsl-variable

在根据节点是否存在设置变量后,我尝试输出变量的文字字符串值。我认为条件检查逻辑是正确的。但它没有输出值...

<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/

相关文章:

html - 需要显示带有等长下划线的多行文本

xml - XSLT 以不同的 xml 读取顺序应用模板

xslt-1.0 - 使用 xsl :value-of in for-each loop over a xsl:variable

java - 创建树的 XSL 问题

xslt - 按值对元素进行排序并删除重复项

xml - 将 XML 元素的名称替换为属性的值

xml - 查找xml文档中价数最高的元素

xslt - 是 xsl :sequence always non-empty?

java - xsl变量初始化,方法不被调用