替换撒克逊人:if和saxon:在xslt 2.0中的函数之前

时间:2009-10-31 14:44:02

标签: xslt xpath saxon

saxon:if是否有替代品 和XSLT 2.0 / XPath 2.0中的saxon:before函数?

我有这样的代码:

<xsl:variable name="stop"
  select="(following-sibling::h:h1|following-sibling::h:h2)[1]" />

<xsl:variable name="between"
  select="saxon:if($stop,
                   saxon:before(following-sibling::*, $stop),
                   following-sibling::*)" />

想法是between变量应该包含当前节点与下一个h1h2元素(存储在stop变量中)之间的所有元素,或者包含所有剩余元素,如果没有下一个h1h2

我想在新的XSLT 2.0模板中使用此代码,我正在寻找saxon:ifsaxon:before的替代品。

3 个答案:

答案 0 :(得分:1)

saxon.if(A, B, C)现在相当于XPath 2.0中的if (A) then B else C

答案 1 :(得分:0)

这是我的解决方案:

<xsl:variable 
     name="stop"
     select="(following-sibling::h:h1|following-sibling::h:h2)[1]" />

<xsl:variable name="between">
    <xsl:choose>
        <xsl:when test="$stop">
            <xsl:sequence select="following-sibling::*[. &lt;&lt; $stop]" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:sequence select="following-sibling::*" />
         </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

它使用来自XSLT 2.0 / XPath 2.0的<xsl:sequence><< operator(编码为&lt;&lt;)。

它不像原始版本那么短,但它不再使用撒克逊扩展。

答案 2 :(得分:0)

您还可以在XSLT / XPath 2.0中只使用一个表达式:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text()"/>
    <xsl:template match="p[position()=(1,3,4)]">
        <xsl:copy-of select="following-sibling::*
                                [not(self::h2|self::h1)]
                                [not(. >>
                                     current()
                                        /following-sibling::*
                                            [self::h2|self::h1][1])]"/>
    </xsl:template>
</xsl:stylesheet>

使用此输入:

<html>
    <p>1</p>
    <p>2</p>
    <h2>Header</h2>
    <p>3</p>
    <h1>Header</h1>
    <p>4</p>
    <p>5</p>
</html>

输出:

<p>2</p><p>5</p>