Reverse things in XML using xslt



Hi I am learning for an exam where we should be able to reverse things in XML. I am trying to reverse the order of books, or reverse the order of elements in those books.


The input XML is:



<library>
<book>
<author>James Joyce</author>
<title>Ulysses</title>
<year>1922</year>
</book>
<book>
<author>Alois Jirasek</author>
<title>Psohlavci</title>
<year>1910</year>
</book>
<book>
<author>Tomas Hruska</author>
<title>Pascal pro zacatecniky</title>
<year>1989</year>
</book>
</library>


Reversed order of books:



<library>
<book>
<author>Tomas Hruska</author>
<title>Pascal pro zacatecniky</title>
<year>1989</year>
</book>
<book>
<author>Alois Jirasek</author>
<title>Psohlavci</title>
<year>1910</year>
</book>
<book>
<author>James Joyce</author>
<title>Ulysses</title>
<year>1922</year>
</book>
</library>


Reversing order of items in books:



<library>
<book>
<year>1922</year>
<title>Ulysses</title>
<author>James Joyce</author>
</book>
<book>
<year>1910</year>
<title>Psohlavci</title>
<author>Alois Jirasek</author>
</book>
<book>
<year>1989</year>
<title>Pascal pro zacatecniky</title>
<author>Tomas Hruska</author>
</book>
</library>


I've been told that it should be quite easy, so don't try anything too fancy. I am allowed to use basic loops via for-each or recursion via apply-templates and also value-of.


No comments:

Post a Comment