There are times when you are about to complete task that is seemingly easy, but somehow you know that you are not doing it right. It’s a feeling that you can get when trying to saw through a wooden beam with a handsaw, while there is a brand new, shiny chainsaw laying nearby. There is only one problem, you have no idea how to use it. And here comes a dilemma, whether to take some time and learn how chainsaw works, or maybe to let it go, and just do it with familiar tool. Learning new equipment is going to take you a lot more time and effort than just quick usage of simpler tool - and there are ominous deadlines. Of course gained knowledge can benefit in a future, when similar task will appear – you chainsaw skill will be ready.

So what to do ? As always there is no good answer… but wait, maybe there is – it depends. But that is a topic for a whole new big article, and we have got some XML waiting.

XSLT – it’s like a giant sawmill for XML, yet it’s light and elegant as handsaw. To understand it deeply you have got to learn your lesson, but fortunately there are useful XSLT snippets flying by, and they might be exactly what you are looking for. And here is my brick in this google an answer move, easy way to copy your XML file, but with sort a node option. So basically we provide an XML file, this XSLT snippet, and as a result we have got same XML file but sorted accordingly to our needs.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
        <xsl:apply-templates select="node()|@*"> 
            <xsl:sort select="author"/> 
        </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet>

Where author is a node by which we want it sorted.

I hope that someone is going to find it useful.

Tags: ,