Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: "Aron Bock" <aronbock@----------->
To:
Date: 7/2/2005 2:46:00 AM
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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent