Is it possible in XSLT-1.0 to sort by a value in the xml. In the example below the <examples> has requested that that the list of <example>s be sorted by the year field.
...
<examples>
<sortby>year</sortby>
<example>
<year>2012</year>
<number>3</number>
</example>
</examples>
...
A static conditional using choose does not work in this case as I will not know what the possible fields in <example> will be.
Yes, use something of this form:
Do note that you need to also specify the sort data-type and order.
Complete example:
When this transformation is applied on the following XML document:
the wanted, correct result is produced:
If the same transformation is applied on this XML document (the same as above, but with changed
sortbyandsort-order):then again the wanted, correct result is produced:
Explanation:
The
selectattribute ofxsl:sortcan contain any XPath expression, so we can specify an expression that selects any child of the elements to be sorted, such that its name is the result of evaluating another XPath expression.In XSLT, typically, all attributes other than
selectallow *AVT*s (Attribute Value Templates) to be specified in their values.