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

Is there a replacement for saxon: if and saxon: before function in XSLT 2.0 / XPath 2.0?

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

The idea is that the variable betweenshould contain all the elements between the current node and next elements h1or h2(stored in the variable stop) or all other elements if the next one h1or h2.

I would like to use this code in the new XSLT 2.0 template, and I'm looking for a replacement for saxon:ifand saxon:before.

0
source share
3 answers

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>

<xsl:sequence> << operator ( &lt;&lt;), XSLT 2.0/XPath 2.0.

, , .

0

saxon.if(A, B, C) if (A) then B else C XPath 2.0

+1

You can also use only 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>
0
source

Source: https://habr.com/ru/post/1721625/


All Articles