Transformation of book information into XHTML with sorting : html « XSLT stylesheet « XML

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XSLT stylesheet » html 
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
1. Output various html tags
2. Output html img tag
3. One html tag per template
4. Wrap HTML tags in template
5. Format output with HTML tags
6. Construct image name used by generated HTML in style sheet
7. html output method to make br tags come out as
8. Output html with frameset
9. Output whole xhtml document
10. Add more format with html tags
11. Format output with font
12. Use blockquote to output value from xml
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.