[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: Sort, group, flatten, consecutive data
Robert,
Here's a demo of what you could do -- it first creates an RTF (result tree fragment) in required order, then walks the tree, emitting elements as it does so. Thus, with this XML: <data> <a n="1" v="1"/> <a n="2" v="1"/> <a n="4" v="2"/> <a n="3" v="1"/> </data> This XSL: <?xml version="1.0" encoding="iso8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="xalan"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <xsl:variable name="sortedNodes"> <xsl:for-each select="data/a"> <xsl:sort data-type="number" order="ascending" select="@v"/> <xsl:sort data-type="number" order="ascending" select="@n"/> <xsl:copy-of select="."/> </xsl:for-each> </xsl:variable> <xsl:variable name="sortedNodeSet" select="xalan:nodeset($sortedNodes)"/> <data> <xsl:call-template name="write-row"> <xsl:with-param name="node" select="$sortedNodeSet/a[1]"/> <xsl:with-param name="n-start" select="$sortedNodeSet/a[1]/@n"/> <xsl:with-param name="v" select="$sortedNodeSet/a[1]/@v"/> </xsl:call-template> </data> </xsl:template> <xsl:template name="write-row"> <xsl:param name="node" select="/.."/> <xsl:param name="n-start"/> <xsl:param name="v"/> <xsl:choose> <xsl:when test="not($node/following-sibling::a[1])"> <row start="{$n-start}" end="{$node/@n}" v="{$v}"/> </xsl:when> <xsl:when test="$v != $node/following-sibling::a[1]/@v"> <row start="{$n-start}" end="{$node/@n}" v="{$v}"/> <xsl:variable name="next-node" select="$node/following-sibling::a[1]"/> <xsl:if test="$next-node"> <xsl:call-template name="write-row"> <xsl:with-param name="node" select="$next-node"/> <xsl:with-param name="n-start" select="$next-node/@n"/> <xsl:with-param name="v" select="$next-node/@v"/> </xsl:call-template> </xsl:if> </xsl:when> <xsl:otherwise> <xsl:call-template name="write-row"> <xsl:with-param name="node" select="$node/following-sibling::a[1]"/> <xsl:with-param name="n-start" select="$n-start"/> <xsl:with-param name="v" select="$v"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> Produces: <?xml version="1.0" encoding="UTF-8"?> <data> <row v="1" end="3" start="1"/> <row v="2" end="4" start="4"/> </data> Regards, --A From: "Midnit" <midnit@xxxxxxxxxxxxxxxxx> Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Subject: Sort, group, flatten, consecutive data Date: Tue, 28 Jun 2005 17:01:18 -0400 _________________________________________________________________ Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/
|
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
|