[Bug 4400] Allow xsl:variable before xsl:sort

http://www.w3.org/Bugs/Public/show_bug.cgi?id=4400

           Summary: Allow xsl:variable before xsl:sort
           Product: XPath / XQuery / XSLT
           Version: Recommendation
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: XSLT 2.0
        AssignedTo: mike@saxonica.com
        ReportedBy: viktor_b_68@hotmail.com
         QAContact: public-qt-comments@w3.org


The XSLT recommendation at http://www.w3.org/TR/xslt#element-sort specifies
that "When used in xsl:for-each, xsl:sort elements must occur first."
That is generally a good rule, but I suggest an exception should be made for
xsl:variable (and possibly others that do not generate content), allowing that
element to appear before xsl:sort.
Because the purpose of variables is to avoid having to print similar code many
times.
But as seen in the examples, due to the current restricion, you have to print
out a long complex search path twice.
In the second (conceptual) example, you could first assign a node to a
variable, then use it in the xsl:sort element already.
The result is a much cleaner document, that is easier to maintain.


<xsl:for-each select="/foo/bar">
        <xsl:sort select="document('items.xml')//item[@xml:id =
current()/@code]/@label" />
        <xsl:variable name="item" select="document('items.xml')//item[@xml:id =
current()/@code]" />  <!-- unnecessary repetion -->
        <xsl:value-of select="$item/@a" />
        ...
        <xsl:value-of select="$item/@z" />
</xsl:for-each>


<xsl:for-each select="/foo/bar">
        <xsl:variable name="item" select="document('items.xml')//item[@xml:id =
current()/@code]" />  <!-- only written once -->
        <xsl:sort select="$item/@label" />
        <xsl:value-of select="$item/@a" />
        ...
        <xsl:value-of select="$item/@z" />
</xsl:for-each>

Received on Tuesday, 20 March 2007 14:59:47 UTC