[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Grouping and Numbering
I find this problem rather difficult to solve with pure XSLT 1.0. This is due to the fact, that XSLT doesn't allow to maintain variable state. If you are willing to write extension functions (for e.g. in Java), it's easier to solve this problem. Please consider the following example. Java extension class: public class Iterator { private static int i = 0; public static int next() { i++; return i; } } XSLT stylesheet: <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xalan/java" exclude-result-prefixes="java" version="1.0"> <xsl:output method="xml" indent="yes" /> <xsl:key name="by-group" match="result" use="details/group_id" /> <xsl:template match="sample"> <sample> <xsl:for-each select="result[generate-id() = generate-id(key('by-group', details/group_id)[1])]"> <xsl:sort select="details/group_id" data-type="number" /> <xsl:variable name="x" select="position()" /> <xsl:for-each select="key('by-group', details/group_id)"> <output row="{java:Iterator.next()}"><xsl:value-of select="details/group_id" /></output> </xsl:for-each> <xsl:if test="$x != last()"> <output row="{java:Iterator.next()}" /> </xsl:if> </xsl:for-each> </sample> </xsl:template> </xsl:stylesheet> Using Xalan-J (ver 2.7.0), when the above stylesheet is applied to the XML: <sample> <result> <details> <group_id>250</group_id> </details> </result> <result> <details> <group_id>300</group_id> </details> </result> <result> <details> <group_id>250</group_id> </details> </result> </sample> The output produced is: <?xml version="1.0" encoding="UTF-8"?> <sample> <output row="1">250</output> <output row="2">250</output> <output row="3"/> <output row="4">300</output> </sample> PS: The stylesheet uses Muenchian method to do grouping. I am also keen to know, is there any way, this problem can be solved with pure XSLT 1.0. On 5/25/07, Carl Radley <Carl.Radley@xxxxxxxxxxx> wrote: Hi, I'm using XSLT version 1, so can't use grouping. -- Regards, Mukul Gandhi
|
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
|