Transformation of book information into XHTML with sorting
File: Data.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<book isbn="999-99999-9-X">
<title>XML Primer</title>
<author>
<firstName>A</firstName>
<lastName>B</lastName>
</author>
<chapters>
<frontMatter>
<preface pages="2" />
<contents pages="5" />
<illustrations pages="4" />
</frontMatter>
<chapter number="3" pages="44">Advanced XML</chapter>
<chapter number="2" pages="35">Intermediate XML</chapter>
<appendix number="B" pages="26">Parsers and Tools</appendix>
<appendix number="A" pages="7">Entities</appendix>
<chapter number="1" pages="28">XML Fundamentals</chapter>
</chapters>
<media type="CD" />
</book>
File: Transform.xslt
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/book">
ISBN:<xsl:value-of select="@isbn" />
Title:<xsl:value-of select="title" />
by<xsl:value-of select="author/lastName"/>,<xsl:value-of select="author/firstName" />
<xsl:for-each select="chapters/frontMatter/*">
<xsl:value-of select="name()" />(<xsl:value-of select="@pages" />pages )
</xsl:for-each>
<xsl:for-each select="chapters/chapter">
<xsl:sort select="@number" data-type="number" order="ascending" />
Chapter: <xsl:value-of select="@number" />(<xsl:value-of select="@pages" />pages )
</xsl:for-each>
<xsl:for-each select="chapters/appendix">
<xsl:sort select="@number" data-type="text" order="ascending" />
Appendix<xsl:value-of select="@number" />(<xsl:value-of select="@pages" />pages )
</xsl:for-each>
<p style="color: blue">
Pages:
<xsl:variable name="pagecount"
select="sum(chapters//*/@pages)" />
<xsl:value-of select="$pagecount" />
<br />
Media Type:
<xsl:value-of select="media/@type" />
</p>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
ISBN:999-99999-9-X
Title:XML Primer
byB,Apreface(2pages )
contents(5pages )
illustrations(4pages )
Chapter: 1(28pages )
Chapter: 2(35pages )
Chapter: 3(44pages )
AppendixA(7pages )
AppendixB(26pages )
<p xmlns="http://www.w3.org/1999/xhtml" style="color: blue">
Pages:
151<br/>
Media Type:
CD</p>
Related examples in the same category