Nested for-each statement : for each « 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 » for each 
Nested for-each statement


File: Data.xml
<?xml version="1.0" ?>

<customer-list>
  <customer>
    <name>
      <first>Peter</first>
      <last>Whimsey</last>
    </name>
    <order>25 cases Earl Grey tea</order>
    <order>12 bottles brandy</order>
  </customer>
  <customer>
    <name>
      <first>Charlie</first>
      <last>Chan</last>
    </name>
    <order>Chinese-English dictionary</order>
    <order>cases mustache wax</order>
  </customer>
  <customer>
    <name>
      <first>Nora</first>
      <last>Charles</last>
    </name>
    <order>bottles wine</order>
    <order>24 sandwiches</order>
  </customer>
  <customer>
    <name>
      <first>Nick</first>
      <last>Charles</last>
    </name>
    <order>12 cases gin</order>
    <order>cartons dog food</order>
  </customer>
</customer-list>

File: Transform.xslt

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

  <xsl:template match="/">
    <HTML>
      <HEAD><TITLE>Customers</TITLE></HEAD>
      <BODY>
        <TABLE BORDER="1" >
          <xsl:for-each select="customer-list/customer">
            <TR><TH ALIGN="left">
              <xsl:apply-templates select="name"/>
            </TH></TR>
            <xsl:for-each select="order">
              <TR><TD>
                <xsl:apply-templates/>
              </TD></TR>
            </xsl:for-each>
          </xsl:for-each>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="name">
    <xsl:value-of select="last" />, 
    <xsl:value-of select="first" />
  </xsl:template>

</xsl:stylesheet>

Output:

<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <TITLE>Customers</TITLE>
   </HEAD>
   <BODY>
      <TABLE BORDER="1">
         <TR>
            <TH ALIGN="left">Whimsey, 
                   Peter
            </TH>
         </TR>
         <TR>
            <TD>25 cases Earl Grey tea</TD>
         </TR>
         <TR>
            <TD>12 bottles brandy</TD>
         </TR>
         <TR>
            <TH ALIGN="left">Chan, 
                   Charlie
            </TH>
         </TR>
         <TR>
            <TD>Chinese-English dictionary</TD>
         </TR>
         <TR>
            <TD>cases mustache wax</TD>
         </TR>
         <TR>
            <TH ALIGN="left">Charles, 
                   Nora
            </TH>
         </TR>
         <TR>
            <TD>bottles wine</TD>
         </TR>
         <TR>
            <TD>24 sandwiches</TD>
         </TR>
         <TR>
            <TH ALIGN="left">Charles, 
                   Nick
            </TH>
         </TR>
         <TR>
            <TD>12 cases gin</TD>
         </TR>
         <TR>
            <TD>cartons dog food</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>

 
Related examples in the same category
1. Use for-each loop
2. Use for-each to loop through each tags
3. for-each loop and table rows
4. for-each select="*", value-of="."
5. for-each select="node()", value-of select="."
6. for-each select="child::parentTagName", value-of select="childTagName"
7. for-each select="child::parentTagName[@attributeName='your value']"
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.