Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xsl] Merging elements within a range using a key

From: "Mike B" <mikeb0304@----------->
To:
Date: 5/2/2006 12:49:00 AM
I have a working transformation which merges Row elements into the 
appropriate Category elements as shown, based on the range



          category/@s <= row/@r <= category/@e



This statement works,   <xsl:copy-of select="/view/results/row[@r &gt;= 
current()/@s and @r &lt;= current()/@e]" />  but seems to slow things down 
as data volume increases.



Is it possible to use a key to find elements within a range like this? I'm 
at a loss as to what the syntax might be.



Any suggestions are appreciated!



Thanks. Mike



Simplified Input document
------------------------------------
<view>
 <results>
   <row r="20">
     <col c="1">col 20.1 data</col>
   </row>
   <row r="21">
     <col c="1">col 21.1 data</col>
   </row>
   <row r="22">
     <col c="1">col 22.1 data</col>
   </row>
 </results>
 <category>
   <category s="18" e="19">cat a</category>
   <category s="20" e="21">cat b</category>
   <category s="22" e="25">cat c</category>
 </category>
</view>


Output document
------------------------
<view>
 <results>
   <category s="20" e="21">cat b
     <row r="20">
       <col c="1">col 20.1 data</col>
     </row>
     <row r="21">
       <col c="1">col 21.1 data</col>
     </row>
   </category>
   <category s="22" e="25">cat c
     <row r="22">
       <col c="1">col 22.1 data</col>
     </row>
   </category>
 </results>
</view>





Transformation - without a key

----------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



   <xsl:output method="xml" encoding="utf-8" />



<xsl:template match="/view">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
  <xsl:copy-of select="/view/category" />
 </xsl:copy>
</xsl:template>

<xsl:template match="results">
 <!--  do not copy old results to output -->
</xsl:template>

<xsl:template match="/view/category">
 <xsl:element name="results">
  <xsl:call-template name="copy-attributes">
   <xsl:with-param name="attributes" select="@*" />
  </xsl:call-template>
  <xsl:call-template name="copy-attributes">
   <xsl:with-param name="attributes" select="/view/results/@*" />
  </xsl:call-template>
  <xsl:apply-templates select="category" />
 </xsl:element>

</xsl:template>



<xsl:template match="category">



 <xsl:variable name="row-test">

  <!-- should be improved with a key -->

  <xsl:copy-of select="/view/results/row[@r &gt;= current()/@s and @r &lt;= 
current()/@e]" />

 </xsl:variable>



 <xsl:if test="string-length($row-test) > 0">
  <xsl:choose>
   <xsl:when test="category">

    <xsl:element name="{name()}">
     <xsl:call-template name="copy-attributes">
      <xsl:with-param name="attributes" select="@*" />
     </xsl:call-template>
     <xsl:value-of select="text()" />

     <xsl:apply-templates select="category"/>



    </xsl:element>
   </xsl:when>

   <xsl:otherwise>
    <xsl:element name="{name()}">
     <xsl:call-template name="copy-attributes">
      <xsl:with-param name="attributes" select="@*" />
     </xsl:call-template>
     <xsl:value-of select="text()" />

     <xsl:copy-of select="$row-test" />
    </xsl:element>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:if>

</xsl:template>




<xsl:template name="copy-attributes">
 <xsl:param name="attributes" />

 <xsl:for-each select="$attributes">
  <xsl:attribute name="{name()}">
   <xsl:value-of select="." />
  </xsl:attribute>
 </xsl:for-each>

</xsl:template>



<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>



_________________________________________________________________

FREE pop-up blocking with the new MSN Toolbar  get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/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