nested for-each loops in xml

Posted by user1748443 on Stack Overflow See other posts from Stack Overflow or by user1748443
Published on 2012-10-15T22:38:37Z Indexed on 2012/10/15 23:02 UTC
Read the original article Hit count: 299

Filed under:
|

I'm new to XML. I'm trying to create table containing item details and another table to contain customer details for each order on a picklist. It seems like it should be straightforward but I just get a list of all items on all orders repeated by the number of orders. What am I doing wrong? (XSL code below...)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" doctype-system="about:legacy-compat"/>
<xsl:template match="/">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="css/text" href="style.css"/>
<title>Orders</title>
</head>

<body>
<xsl:for-each select="//order">
<table>
<caption><h3>Order Information</h3></caption>
<thead>
<th align="left">Item Id</th>
<th align="left">Item Description</th>
<th align="left">Quantity</th>
<th align="left">Price</th>
</thead>
<xsl:for-each select="//item">
<tr>
<td align="left"><xsl:value-of select="itemId"/></td>
<td align="left"><xsl:value-of select="itemName"/></td>
<td align="left"><xsl:value-of select="quantity"/></td>
<td align="left"><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
<table>
<caption><h3>Customer Information</h3></caption>
<thead>
<th align="left">Customer Name</th>
<th align="left">Street</th>
<th align="left">City</th>
</thead>
<xsl:for-each select="//item">
<tr>
<td align="left"><xsl:value-of select="customerName"/></td>
<td align="left"><xsl:value-of select="street"/></td>
<td align="left"><xsl:value-of select="city"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

This is the XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Orders.xsl"?>

<orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Orders.xsd">
<order>
<orderId>123</orderId>
<items>
<item>
<itemId>001</itemId>
<itemName>Nylon Rope</itemName>
<quantity>1</quantity>
<price>3.50</price>
</item>
<item>
<itemId>002</itemId>
<itemName>Shovel</itemName>
<quantity>1</quantity>
<price>24.95</price>
</item>
</items>
<customerAddress>
<customerName>Larry Murphy</customerName>
<street>Shallowgrave Lane</street>
<city>Ballymore Eustace, Co. Kildare</city>
</customerAddress>
</order>
<order>
<orderId>124</orderId>
<items>
<item>
<itemId>001</itemId>
<itemName>Whiskey</itemName>
<quantity>1</quantity>
<price>18.50</price>
</item>
<item>
<itemId>002</itemId>
<itemName>Shotgun</itemName>
<quantity>1</quantity>
<price>225</price>
</item>
<item>
<itemId>003</itemId>
<itemName>Cartridge</itemName>
<quantity>1</quantity>
<price>1.85</price>
</item>
</items>
<customerAddress>
<customerName>Enda Kenny</customerName>
<street>A Avenue</street>
<city>Castlebar, Co. Mayo</city>
</customerAddress>
</order>
</orders>

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt