I've this xml code:
<items>
<item name="xyz_low" />
<item name="xyz_hight" />
<item name="xyz_medium" />
<item name="abc_medium" />
<item name="abc_low" />
<item name="abc_hight" />
</items>
I would like to get something like:
<items>
<item name="xyz_hight" />
<item name="xyz_medium" />
<item name="xyz_low" />
<item name="abc_hight" />
<item name="abc_medium" />
<item name="abc_low" />
</items>
The sorting happens only between tags with the same first part of the attribute name (the part before the underscore). The order is hight, medium and low.
I've look at ends-with
, but it's not enough for me because I don't know if the first part could be xyz, abc or something else.
I don't need to sort the first part: I just want to sort the suffixes within the group of item tags starting in the same way.
Is it possible or I should choose a different approach?
To sort items in a certain fixed order like this you can use a trick like this: define a variable for the sort order
and now you can sort using
for whatever
value
you need. If thevalue
is "hight" then thesubstring-before
will be of length 0, for "medium" it'll be of length 6, etc.Here's an XSLT 2.0 template that'll do what you're after, assuming that variable definition
To do the same in XSLT 1.0 you'd need to do a Muenchian grouping trick
and then in the template
(the XSLT 2.0 version still works if there are several different sets of
items
in the source document, the XSLT 1.0 version would get a lot messier in that case).