RE: [xsl] Sort, group, flatten, consecutive data

Subject: RE: [xsl] Sort, group, flatten, consecutive data
From: "Aron Bock" <aronbock@xxxxxxxxxxx>
Date: Sat, 02 Jul 2005 02:45:44 +0000
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: [xsl] Sort, group, flatten, consecutive data
Date: Tue, 28 Jun 2005 17:01:18 -0400

This is my first post here; I apologize if it's inappropriate.

I'm attempting to flatten and consolidate this XML document. In short I
would sort by DETAIL_ID and PRODUCTIONDAY, then group up based on the RATE,
outputting BEGINDAY and ENDDAY to represent the date range the RATE covered.
Though not shown in this example of data, there will be a record for every
day in the month so sorting by PRODUCTIONDAY will always produce consecutive
rows. I have no problem flattening the data but the attempts I've made at
grouping the data have failed miserably. All the examples I have found use
preceding-sibling and following-sibling which doesn't apply for my data, at
least that's my assumption. I have also tried using keys but I have run into
similar problems.



_________________________________________________________________
Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/


Current Thread