Search code examples
xmlxmllint

Sorting XML by subnode content


I have an XML document with the following structure

<movements status="1">
    <movement>
        <article>49-000003</article>
        <lot>0I0311</lot>
        <type>4</type>
        <date></date>
    </movement>
    <movement>
        <article>49-000013</article>
        <lot>0I0312</lot>
        <type>4</type>
        <date></date>
    </movement>
    <movement>
        <article>49-000001</article>
        <lot>0I0313</lot>
        <type>4</type>
        <date></date>
    </movement>
</movements>

How can I (using xmllint) sort it so the movement nodes are ordered by the article?

I need the output to be another XML file.


Solution

  • Try something like this on your iMac and see if it works:

    xidel  your_file.xml --xquery 'for $node in //movement order by $node/article return $node' --output-format xml