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
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="' '"/> </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
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 "//f" -s A:N:U "concat(@a,concat('|',@m))" -c "."
Log in to post a comment.
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
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="' '"/>
</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
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 "//f" -s A:N:U "concat(@a,concat('|',@m))" -c "."