Menu

Sorting on Multiple Elements

Help
Gadrin
2005-08-17
2013-03-13
  • Gadrin

    Gadrin - 2005-08-17

    is it possible to sort a node-set on multiple elements or attributes using XMLStarlet ?

    you wouldn't happen to have a sample command line example ?

    Thanks, Gadrin

     
    • Mikhail Grushinskiy

      For an input XML

      <x>
          <a p='1'>1</a>
          <t p='2'>4</t>
          <t p='1'>3</t>
          <b>2</b>
      </x>

      you cound apply a stylesheet

      <?xml version="1.0"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:template match="/">
      <xsl:for-each select="x/*">
      <xsl:sort select="name()"/>
      <xsl:sort select="@p"/>
      <xsl:value-of select="."/>
      <xsl:value-of select="'&#10;'"/>
      </xsl:for-each>
      </xsl:template>
      </xsl:stylesheet>

      to print

      1
      2
      3
      4

      I can see a problem with 'select' option. It should have allowed something like this:

      xml sel -C -t -m "x/*" -s A:T:U "name()" -s A:N:L "@p" -v . -n to-sort.xml

      Unfortunately currently it allows only one -s (--sort) after
      -m. I would say it is a bug and should be fixed in next release.

      Thanks,
      --MG

       
  • Giles Cope

    Giles Cope - 2009-09-30

    You could concaternate the values that you'd like to sort on for text (with a deliminator), though this would not work for numbers.

    xml ls | xml sel -t -m &quot;//f&quot; -s A:N:U &quot;concat(@a,concat('|',@m))&quot; -c &quot;.&quot;

     

Log in to post a comment.

MongoDB Logo MongoDB