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>1 Chinese-English dictionary</order>
<order>5 cases mustache wax</order>
</customer>
<customer>
<name>
<first>Nora</first>
<last>Charles</last>
</name>
<order>5 bottles wine</order>
<order>24 sandwiches</order>
</customer>
<customer>
<name>
<first>Nick</first>
<last>Charles</last>
</name>
<order>12 cases gin</order>
<order>4 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>1 Chinese-English dictionary</TD>
</TR>
<TR>
<TD>5 cases mustache wax</TD>
</TR>
<TR>
<TH ALIGN="left">Charles,
Nora
</TH>
</TR>
<TR>
<TD>5 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>4 cartons dog food</TD>
</TR>
</TABLE>
</BODY>
</HTML>
Related examples in the same category