Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Chewy key problem

From: Mukul Gandhi <mukul_gandhi@--------->
To:
Date: 3/1/2005 9:27:00 AM
I feel its not possible with pure XSLT 1.0 . We need
to take help of extension function (node-set). Below
is the solution..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0" xmlns:exslt="http://exslt.org/common"
  exclude-result-prefixes="exslt">

<xsl:output method="xml" indent="yes" />

<xsl:key name="by-desc" match="b"
use="concatenated-desc" />

<xsl:template match="/a">
  <xsl:variable name="rtf">
    <a>
      <xsl:for-each select="b">
        <b>
          <concatenated-desc>
            <xsl:call-template
name="concatenate-desc">
              <xsl:with-param name="x"
select=".//desc" />
              <xsl:with-param name="y" select="''" />
            </xsl:call-template>
          </concatenated-desc>
          <xsl:copy-of select="child::node()" />
        </b>
      </xsl:for-each>
    </a>
  </xsl:variable>
  
  <a>
    <xsl:for-each
select="exslt:node-set($rtf)/a/b[generate-id(.) =
generate-id(key('by-desc',concatenated-desc)[1])]">
      <group-of-b>        
        <xsl:for-each
select="key('by-desc',concatenated-desc)">
          <b>
            <xsl:copy-of
select="child::node()[not(self::concatenated-desc)]"
/>
          </b>
        </xsl:for-each>
      </group-of-b>
    </xsl:for-each>
  </a>
</xsl:template>

<xsl:template name="concatenate-desc">
  <xsl:param name="x" />
  <xsl:param name="y" />
  
  <xsl:choose>
    <xsl:when test="count($x) &gt; 0">
      <xsl:call-template name="concatenate-desc">
        <xsl:with-param name="x" select="$x[position()
&gt; 1]" />
        <xsl:with-param name="y"
select="concat($y,normalize-space($x[1]))" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$y" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

For e.g. when the above XSL is applied to XML -

<a>
  <b>
   <desc>some text
   </desc>
   <bChild>
     <desc>some other text
     </desc>
   </bChild>
  </b>
  <b>
   <desc>some text
   </desc>
   <bChild>
     <desc>some other text
     </desc>
   </bChild>
   <bChild>
     <desc>maybe some more text
     </desc>
   </bChild>
  </b>
  <b>
   <desc>some text
   </desc>
   <bChild>
     <desc>some other text
     </desc>
   </bChild>
   <test>xyz</test>
  </b>
</a>

The output recieved is -

<?xml version="1.0" encoding="UTF-8"?>
<a>
   <group-of-b>
      <b>
        <desc>some text
        </desc>
        <bChild>
          <desc>some other text
          </desc>
        </bChild>
      </b>
      <b>
         <desc>some text
         </desc>
         <bChild>
           <desc>some other text
           </desc>
         </bChild>
         <test>xyz</test>
      </b>
   </group-of-b>
   <group-of-b>
      <b>
        <desc>some text
        </desc>
        <bChild>
          <desc>some other text
          </desc>
        </bChild>
        <bChild>
          <desc>maybe some more text
          </desc>
        </bChild>
      </b>
   </group-of-b>
</a>

Regards,
Mukul

--- Edmund Mitchell <emitchell@xxxxxxxx> wrote:

> So b elements can have description children (desc),
> and so can their bChild
> children.
> 
> The goal is to Meunchian group all the b elements on
> the concatenated value
> of their desc and the desc values of however many
> bChild children there are.



	
		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail


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