Re: [xsl] Chewy key problem

Subject: Re: [xsl] Chewy key problem
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Tue, 1 Mar 2005 01:26:29 -0800 (PST)
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

Current Thread