[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: for-each-group - only get elements in each group
At 2016-01-20 17:35 +0000, Rick Quatro rick@xxxxxxxxxxxxxx wrote:
Hi, I have this XML: ... I want to group on each unique <Category> element and then only include the <Case> elements for each <Category>. Here is what I need: ... My take on it is below. As I see it, your focus is on the cases and what categorizes them is the closest preceding sibling. This would allow for an arbitrary number of case elements after each heading. I hope this is helpful. . . . . . . Ken t:\ftemp>type rick.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Cases> <Story> <Category>Category1</Category> <Case>Case1</Case> <Category>Category1</Category> <Case>Case2</Case> <Category>Category1</Category> <Case>Case3</Case> <Category>Category1</Category> <Case>Case4</Case> <Category>Category2</Category> <Case>Case5</Case> <Category>Category2</Category> <Case>Case6</Case> <Category>Category2</Category> <Case>Case7</Case> <Category>Category2</Category> <Case>Case8</Case> </Story> </Cases> t:\ftemp>xslt2 rick.xml rick.xsl <?xml version="1.0" encoding="UTF-8"?> <cases> <category>Category1</category> <Case>Case1</Case> <Case>Case2</Case> <Case>Case3</Case> <Case>Case4</Case> <category>Category2</category> <Case>Case5</Case> <Case>Case6</Case> <Case>Case7</Case> <Case>Case8</Case> </cases> t:\ftemp>type rick.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:output indent="yes"/> <xsl:template match="/"> <cases> <xsl:for-each-group select="Cases/Story/Case" group-by="preceding-sibling::Category[1]"> <category> <xsl:apply-templates select="preceding-sibling::Category[1]/node()"/> </category> <xsl:copy-of select="current-group()"/> </xsl:for-each-group> </cases> </xsl:template> </xsl:stylesheet> t:\ftemp> -- Check our site for free XML, XSLT, XSL-FO and UBL developer resources | Free 5-hour lecture: http://www.CraneSoftwrights.com/links/video.htm | Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ | G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx | Google+ profile: http://plus.google.com/+GKenHolman-Crane/about | Legal business disclaimers: http://www.CraneSoftwrights.com/legal |
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|