![]() |
![]() | ![]() | ![]() | Altova Mailing List ArchivesRe: Sorting and Grouping within XSLTo: NULL Date: 11/22/2007 5:46:00 PM S wrote: > I have this xml document see below what I want to do is loop through > each category in the ArrayOfCategory and for each CategoryID I need > to > get the list of ShoppingLIstDetail for that CategoryID Here is an XSLT 1.0 example that makes use of xsl:key and the key function: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="by-id" match="ShoppinglistDetail" use="CategoryID"/> <xsl:template match="/"> <Categories> <xsl:apply-templates select="PrintList/ArrayOfCategory/Category"/> </Categories> </xsl:template> <xsl:template match="Category"> <xsl:copy> <xsl:copy-of select="*"/> <xsl:apply-templates select="key('by-id', CategoryID)"/> </xsl:copy> </xsl:template> <xsl:template match="ShoppinglistDetail"> <xsl:copy> <xsl:copy-of select="*[not(self::CategoryID)]"/> </xsl:copy> </xsl:template> </xsl:stylesheet> Result when applied to you XML input is <Categories> <Category xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <CategoryID>10337</CategoryID> <CategoryName>Bakery</CategoryName> <ShoppinglistDetail> <ItemDescription>Item 1</ItemDescription> <Quantity>1</Quantity> <SalePrice>1.000000000</SalePrice> </ShoppinglistDetail> <ShoppinglistDetail> <ItemDescription>Item 3</ItemDescription> <Quantity>1</Quantity> <SalePrice>1.000000000</SalePrice> </ShoppinglistDetail> <ShoppinglistDetail> <ItemDescription>Item 4</ItemDescription> <Quantity>5</Quantity> <SalePrice>1.500000000</SalePrice> </ShoppinglistDetail> </Category> <Category xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <CategoryID>10338</CategoryID> <CategoryName>Dairy</CategoryName> <ShoppinglistDetail> <ItemDescription>Item 2</ItemDescription> <Quantity>10</Quantity> <SalePrice>1.000000000</SalePrice> </ShoppinglistDetail> </Category> </Categories> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
