This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.
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>
Thanks for the comment. We haven't really started any serious work yet towards gathering requirements for a future version of XSLT, but no doubt this suggestion will get added into the list for consideration. The idea seems to take one a step closer to reproducing the XQuery FLWOR expression in XSLT. In XQuery it would be written: for $x in /foo/bar let $item := document('items.xml')//item[@xml:id = $x/@code] order by $item/label return $item/@a, $item/@z In your particular example (and in most practical examples) I think there are straightforward workarounds. Here I would do: <xsl:key name="k" match="item" use="@xml:id"/> then <xsl:for-each select="/foo/bar/key('k', @code, document('items.xml'))"> <xsl:sort select="label"/> <xsl:value-of select="@a"/> <xsl:value-of select="@z"/> This is a personal response.
This will be keep for possible consideration as a future enhancement.
*** This bug has been marked as a duplicate of bug 5166 ***