Replacement for saxon:if and saxon:before functions in xslt 2.0

谁都会走 提交于 2019-12-24 18:52:16

问题


Is there any replacement for saxon:if and saxon:before functions in XSLT 2.0 / XPath 2.0?

I have code like this:

<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::*)" />

Idea is that between variable should contain all elements between current node and next h1 or h2 element (stored in stop variable), or all remaining elements, if there is no next h1 or h2.

I'd like to use this code in new XSLT 2.0 template, and I am looking for replacement for saxon:if and saxon:before.


回答1:


saxon.if(A, B, C) is now equivalent to if (A) then B else C in XPath 2.0




回答2:


Here is my solution:

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

It uses <xsl:sequence> and << operator (encoded as &lt;&lt;), from XSLT 2.0 / XPath 2.0.

It's not as short as original version, but it doesn't use saxon extensions anymore.




回答3:


You also could use just one expression in 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>

With this input:

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

Output:

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


来源:https://stackoverflow.com/questions/1654585/replacement-for-saxonif-and-saxonbefore-functions-in-xslt-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!