Use xslt style sheet to output data in a table : table « 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 » table 
Use xslt style sheet to output data in a table



<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
                xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
                version="1.0">
 
    <xsl:output method="html"/>
                    
    <xsl:variable name="schemaLocation" select="substring-after(/*/@xsi:schemaLocation,' ')"/>
    <xsl:variable name="schema" select="document($schemaLocation)"/>
    <xsl:variable name="instance" select="/"/>

    <xsl:template match="/">
        <HTML>
            <HEAD>
                <TITLE>Welcome</TITLE>
            </HEAD>
            <BODY>
                <xsl:apply-templates select="$schema//xsd:complexType[@name]"/>
            </BODY>
        </HTML>
    </xsl:template>

    <xsl:template match="xsd:complexType[@name]">
        <table border="1" width="50%">
            <tr><th colspan="3" align="center">Metadata for this Resource: <xsl:value-of select="@name"/></th></tr>
            <tr><th>Property</th><th>Type</th><th>Value</th></tr>
        <xsl:apply-templates/>
        </table>
    </xsl:template>

    <xsl:template match="xsd:element">    
        <xsl:variable name="name" select="@name"/>   
        <xsl:variable name="type" select="@type"/>
        <xsl:if test="not(@maxOccurs)">
            <tr>
                <td align="center"><xsl:value-of select="$name"/></td>
                <td align="center"><xsl:value-of select="$type"/></td>
                <td align="center"><xsl:value-of select="$instance//*[name(.)=$name]"/></td>
            </tr>
        </xsl:if>   
        <xsl:if test="@maxOccurs">   
            <xsl:if test="@maxOccurs=1">
                <tr>
                    <td align="center"><xsl:value-of select="$name"/></td>
                    <td align="center"><xsl:value-of select="$type"/></td>
                    <td align="center"><xsl:value-of select="$instance//*[name(.)=$name]"/></td>
                </tr>
            </xsl:if>    
            <xsl:if test="@maxOccurs &gt; 1">
                <xsl:for-each select="$instance//*[name(.)=$name]">
                    <tr>
                        <td align="center"><xsl:value-of select="$name"/></td>
                        <td align="center"><xsl:value-of select="$type"/></td>
                        <td align="center"><xsl:value-of select="."/></td>
                    </tr>
                </xsl:for-each>
            </xsl:if>      
            <xsl:if test="@maxOccurs='unbounded'">
                <xsl:for-each select="$instance//*[name(.)=$name]">
                    <tr>
                        <td align="center"><xsl:value-of select="$name"/></td>
                        <td align="center"><xsl:value-of select="$type"/></td>
                        <td align="center"><xsl:value-of select="."/></td>
                    </tr>
                </xsl:for-each>
            </xsl:if>  
        </xsl:if>  
    </xsl:template>

</xsl:stylesheet>

 
Related examples in the same category
1. Output to a table
2. for-each loop and table output
3. Sort a column
4. Use for-each to output table rows
5. select value for table cell
6. Get value with value-of for table cell
7. Use for-each to loop through nodes in certain level
8. Fill more one value into table cell
9. use
to format value in a table cell
10. Create table header and content in separated templates
11. One template per table row
12. Add row number
13. Create a table with sorting
14. number column
15. Generate two tables
16. Create table header
17. Sort first then output to table
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.