使用条件将XSL变量设置为节点集

时间:2013-03-04 05:34:05

标签: xslt

这个问题与XSL store node-set in variable非常相似。主要区别在于,如果XPath没有找到与过滤器匹配的节点,我想返回第一个未过滤的结果。

我在这里的代码有效,但我觉得这是一个黑客而且不是很好的XSL风格。在这种情况下,每个章节点由字符串id标识。变量showChapter是标识章节的字符串。如果没有找到这个id属性的章节,我想返回第一章。

相关代码:

<xsl:param name="showChapter" />

<!-- if $showChapter does not match any chapter id attribute, 
     set validShowChapter to id of first chapter. 
-->

<xsl:variable name="validShowChapter">
    <xsl:choose>
        <xsl:when test="/book/chapter[@id=string($showChapter)][position()=1]">
            <xsl:value-of select="$showChapter" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="/book/chapter[position()=1]/@id" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<!-- I want $chapter to be a valid node-set so I can use it in 
     XPath select statements in my templates 
-->
<xsl:variable 
    name="chapter"
    select="/book/chapter[@id=string($validShowChapter)][position()=1]"
>

这种方法是否像我认为的那样糟糕,如果是这样,你能指出我更好的解决方案吗?我正在使用由PHP5的XSLTProcessor处理的XSLT 1.0,但欢迎使用XSLT 2.0解决方案。

1 个答案:

答案 0 :(得分:1)

以下内容应该有效。你的例子中很多position()string()的使用都是不必要的,顺便说一下:

<xsl:param name="showChapter" />

<xsl:variable name="foundChapter" select="/book/chapter[@id = $showChapter]" />
<!-- Will select either the first chapter in $foundChapter, or
     the first chapter available if $foundChapter is empty -->
<xsl:variable name="chapter" 
              select="($foundChapter | /book/chapter[not($foundChapter)])[1]" />