【问题标题】:Replacement for saxon:if and saxon:before functions in xslt 2.0xslt 2.0 中 saxon:if 和 saxon:before 函数的替换
【发布时间】:2010-12-11 21:26:31
【问题描述】:

saxon:if 有什么替代品吗? 和saxon:before XSLT 2.0 / XPath 2.0 中的函数?

我有这样的代码:

<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 的替代品。

【问题讨论】:

    标签: xslt xpath saxon


    【解决方案1】:

    您也可以在 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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        这是我的解决方案:

        <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 的 &lt;xsl:sequence&gt;&amp;lt;&amp;lt; operator(编码为 &amp;lt;&amp;lt;)。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-06
          相关资源
          最近更新 更多