Sort the subtree and save it in the xsl variable:

I am working with an XSL stylesheet and I am trying to use the method shown here to save a sorted subtree as a variable. I am using saxon 8.7 in xml-maven-plugin to convert my xml file. Here is the code I have:

<xsl:variable name="miniDays">
    <xsl:for-each select="//day[position() > $firstPosToShow]">
        <xsl:sort select="@date" order="descending" />
        <xsl:copy-of select=".|@*" />
    </xsl:for-each>
</xsl:variable>

When I run the stylesheet, I get the following error:

Error at xsl:copy-of on line 598 of file:/D:/home/Projects/src/main/xsl/site.xsl:
  XTDE0420: Cannot create an attribute node (date) whose parent is a document node

If I just set the subtree of the variable without sorting, it works, but it doesn't sort:

<xsl:variable name="miniDays" select="//day[position() > $firstPosToShow]" />

If I set the copy-of statement, select ".", It will go past this point, but later on I will get an error when I really try to use the variable data. Here's how it is used:

<xsl:for-each select="exsl:node-set($miniDays)">
    <xsl:variable name="in" select="local:calculate-total-in-days(.)" />
    <!-- do some stuff with the var -->
</xsl:for-each>

And the error:

Error on line 676 of file:/D:/home/Projects/src/main/xsl/site.xsl:
  XPTY0004: Required item type of first argument of local:calculate-total-in-days() is element(); supplied value has item type document-node()

Function:

<xsl:function name="local:calculate-total-in-days">
    <xsl:param name="days" as="element()*" />
    <!-- Do some calculations -->
</xsl:function>

Am I using exsl: node-set incorrectly? And what should be in the copy-select, "." or ". @*"?

+3
1

:

  • <xsl:for-each select="//day[position() > $firstPosToShow]">. day , $firstPosToShow+1 day children ! , (//day)[position() >= $firstPosToShow]

  • <xsl:copy-of select=".|@*" />. , . , . , (), node . : <xsl:copy-of select="." />

  • :

           

exsl:node-set($miniDays) document-node(), <xsl:for-each> () node. , local:calculate-total-in-days(.) -, node.

:

<xsl:for-each select="exsl:node-set($miniDays)/*"> 
    <xsl:variable name="in" select="local:calculate-total-in-days(.)" /> 
    <!-- do some stuff with the var --> 
</xsl:for-each>

, exslt:node:set() XSLT 2.0, XSLT 2.0 RTF, Saxon 9.x. , :

<xsl:for-each select="$miniDays/*"> 
    <xsl:variable name="in" select="local:calculate-total-in-days(.)" /> 
    <!-- do some stuff with the var --> 
</xsl:for-each>

, $miniDays element()*, - $miniDays/* - $miniDays

+1

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


All Articles