I have a set of elements with different attributes, like this:
<?xml version="1.0" encoding=utf-8"?>
<root>
<elem at="4" iterate="third" />
<foo/>
<elem at="1" iterate="first" />
<bar/>
<elem start="3" end="9" iterate="second" />
</root>
I want to iterate over the elem tags, so I specified the for-each like this:
<xsl:for-each select="/root/elem">
so far so good, but now I want to sort these elements, according to their @at and @start field, while each elem is guaranteed to have either @at or @start. I annotated in the @iterate attribute how I want the tags to be iterated over (just for the sake of clarity here).
My approach was this
<xsl:sort select="@at or @start" data-type="number" order="ascending" />
But that doesn’t seem to work as intended as elements with an @at attribute seem to be prioritised over elements with an @start attribute.
So, how can I tell XSLT (1.0) not to distinguish between the name of the attribute that is set and simply sort according to its respective value?
You are not far off. Try using the union operator |
Here is the full XSLT
When applied to your XML, the following is output