Is it possible to sort XML files like the following:
<model name="ford">
<driver>Bob</driver>
<driver>Alice</driver>
</model>
<model name="audi">
<driver>Carly</driver>
<driver>Dean</driver>
</model>
Which would become
<model name="audi">
<driver>Carly</driver>
<driver>Dean</driver>
</model>
<model name="ford">
<driver>Alice</driver>
<driver>Bob</driver>
</model>
That is, the outermost elements are sorted first, then the second outermost, and so on.
They'd need to be sorted by element name first. Can this be done? Or should I use something like BeautifulSoup to spin my own?
Try this XSLT:
You can sort nodes by removing them from the parent node, and re-inserting them in the intended order. For example:
You don't need to sort the entire xml dom. Instead take the required nodes into a list and sort them. Because we would need the sorted order while processing and not in file, its better done in run time. May be like this, using minidom.
This is a refinement of Kirill's solution, I think it better reflects the stated requirements, and it avoids the type error XSLT 2.0 will give you if the sort key contains more than one value (but it still works on 1.0).