|
[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
Hi Ken,
This works, but I made a change to generalize it a bit, because each
<Category> element may have other siblings that I need to capture. Here is
what I have, based on your code:
<?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/*"
group-by="preceding-sibling::Category[1]">
<category>
<xsl:apply-templates
select="preceding-sibling::Category[1]/node()"/>
</category>
<xsl:copy-of select="current-group()[not(self::Category)]"/>
</xsl:for-each-group>
</cases>
</xsl:template>
</xsl:stylesheet>
So, if I have this input:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Cases>
<Story>
<Category>Category1</Category>
<Case>Case1</Case>
<Author>Author1</Author>
<Code>Code1</Code>
<Category>Category1</Category>
<Case>Case2</Case>
<Author>Author2</Author>
<Category>Category1</Category>
<Case>Case3</Case>
<Author>Author3</Author>
<Category>Category1</Category>
<Case>Case4</Case>
<Author>Author4</Author>
<Category>Category2</Category>
<Case>Case5</Case>
<Author>Author5</Author>
<Category>Category2</Category>
<Case>Case6</Case>
<Author>Author6</Author>
<Category>Category2</Category>
<Case>Case7</Case>
<Author>Author7</Author>
<Category>Category2</Category>
<Case>Case8</Case>
<Author>Author8</Author>
<Code>Code8</Code>
</Story>
</Cases>
I get this desired output:
<?xml version="1.0" encoding="UTF-8"?>
<cases>
<category>Category1</category>
<Case>Case1</Case>
<Author>Author1</Author>
<Code>Code1</Code>
<Case>Case2</Case>
<Author>Author2</Author>
<Case>Case3</Case>
<Author>Author3</Author>
<Case>Case4</Case>
<Author>Author4</Author>
<category>Category2</category>
<Case>Case5</Case>
<Author>Author5</Author>
<Case>Case6</Case>
<Author>Author6</Author>
<Case>Case7</Case>
<Author>Author7</Author>
<Case>Case8</Case>
<Author>Author8</Author>
<Code>Code8</Code>
</cases>
Thank you for your generous help.
Rick
|
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
|

Cart








