RE: [xsl] Concatenating the attribute values of ancestors

Subject: RE: [xsl] Concatenating the attribute values of ancestors
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Sat, 9 Jul 2005 01:09:14 -0700 (PDT)
Thanks Mike for your help. I changed the stylesheet as
below. I have now introduced another parameter to the
named template called "element", which is the context
node during each recursive call. 

But I don'nt know if my stylesheet will help the
original poster.

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

<xsl:output method="xml" indent="yes" />
  
<xsl:template match="node() | @*" priority="5">
  <xsl:copy>
    <xsl:apply-templates select="node() | @*" />
  </xsl:copy>
</xsl:template>    
  
<xsl:template match="*" priority="6">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:attribute name="newSeq">
        <xsl:call-template
name="concatenate-ancestor-attributes">
           <xsl:with-param name="attr_value"
select="''" />
           <xsl:with-param name="element" select="."
/>
           <xsl:with-param name="ancestors"
select="ancestor::*" />
        </xsl:call-template>
      </xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
  
<xsl:template name="concatenate-ancestor-attributes">
    <xsl:param name="attr_value" />
    <xsl:param name="element" />
    <xsl:param name="ancestors" />
    
    <xsl:choose>
      <xsl:when test="$element/parent::*">
        <xsl:call-template
name="concatenate-ancestor-attributes">
	  <xsl:with-param name="attr_value"
select="concat($attr_value, $element/parent::*/@seq)"
/>
	  <xsl:with-param name="element"
select="$element/parent::*" />
	  <xsl:with-param name="ancestors"
select="$element/ancestor::*" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$attr_value" />
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>
  
</xsl:stylesheet>

Regards,
Mukul


--- Michael Kay <mike@xxxxxxxxxxxx> wrote:

> Yes, this is a bug in your code.
> 
> Your recursive call of the named template doesn't
> change the context node,
> and therefore the expression "ancestor::*" will
> select the same nodes each
> time; and test="parent::*" will return true each
> time if it returns true the
> first time. 
> 
> Michael Kay
> http://www.saxonica.com/
> 
> 
> > -----Original Message-----
> > From: Mukul Gandhi [mailto:mukul_gandhi@xxxxxxxxx]
> 
> > Sent: 08 July 2005 09:54
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > Subject: Re: [xsl] Concatenating the attribute
> values of ancestors
> > 
> > I am actually getting error Exception in thread
> "main"
> > java.lang.OutOfMemoryError with my stylesheet. It
> > seems recursing is not stopping.
> > 
> > I am using JRE 1.4.2_03. I am using Saxon 8.4.
> Could
> > this be a bug?
> > 
> > Regards,
> > Mukul
> > 
> > --- Mukul Gandhi <mukul_gandhi@xxxxxxxxx> wrote:
> > 
> > > Probably some sort of following logic is
> required..
> > > Please be beware, this is not tested.
> > > 
> > > <?xml version="1.0" encoding="UTF-8"?>
> > > <xsl:stylesheet
> > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>    
> > > 
> > > version="1.0">
> > > 
> > > <xsl:output method="xml" indent="yes" />
> > >   
> > > <xsl:template match="node() | @*" priority="5">
> > >   <xsl:copy>
> > >     <xsl:apply-templates select="node() | @*" />
> > >   </xsl:copy>
> > > </xsl:template>    
> > >   
> > > <xsl:template match="*" priority="6">
> > >     <xsl:copy>
> > >       <xsl:apply-templates select="@*" />
> > >       <xsl:attribute name="newSeq">
> > >         <xsl:call-template
> > > name="concatenate-ancestor-attributes">
> > >            <xsl:with-param name="attr_value"
> > > select="''" />
> > >            <xsl:with-param name="anestors"
> > > select="ancestor::*" />
> > >         </xsl:call-template>
> > >       </xsl:attribute>
> > >       <xsl:apply-templates/>
> > >     </xsl:copy>
> > > </xsl:template>
> > >   
> > > <xsl:template
> > > name="concatenate-ancestor-attributes">
> > >     <xsl:param name="attr_value" />
> > >     <xsl:param name="ancestors" />
> > >     
> > >     <xsl:choose>
> > >       <xsl:when test="parent::*">
> > >         <xsl:call-template
> > > name="concatenate-ancestor-attributes">
> > > 	  <xsl:with-param name="attr_value"
> > > select="concat($attr_value, parent::*/@seq)" />
> > > 	  <xsl:with-param name="anestors"
> > > select="ancestor::*" />
> > >         </xsl:call-template>
> > >       </xsl:when>
> > >       <xsl:otherwise>
> > >         <xsl:value-of select="$attr_value" />
> > >       </xsl:otherwise>
> > >     </xsl:choose>
> > > </xsl:template>
> > >   
> > > </xsl:stylesheet>
> > > 
> > > Regards,
> > > Mukul
> > > 
> > > 
> > > --- Lakshmi narayana <lchintala@xxxxxxxxxxxx>
> wrote:
> > > 
> > > > 
> > > > Hi
> > > > I want a template which adds the attribute
> values
> > > of
> > > > its ancestors and
> > > > create an attribute to the current
> > > > node. The value to this attribute is the
> result of
> > > > conactination of all the
> > > > node values added of its parents.
> > > > For example,
> > > > <A seq="1">
> > > > 	<B seq="2" />
> > > > 	<C seq="3">
> > > > 		<D seq="4" />
> > > > 	</C>
> > > > </A>
> > > > In the above xml, I want to concatenate all
> the
> > > seq
> > > > values of all ancestors
> > > > and its seq value also. And add new attribute
> > > > to the current node as newSeq with the result
> > > > concatenated value.
> > > > Note : if seq attribute is not there
> concatenate
> > > > value "0".
> > > > Output should look like this.
> > > > <A seq="1" newSeq="1">
> > > > 	<B seq="2" newSeq="12"/>
> > > > 	<C seq="3" newSeq="13">
> > > > 		<D seq="4"  newSeq="134"/>
> > > > 	</C>
> > > > 	<E  newSeq="10">
> > > > 		<F seq="5"  newSeq="105"/>
> > > > 	</E>
> > > > </A>
> > > > 
> > > > Thanks
> > > > Laxmi Narayana
> > > > 
> > > > 
> > > 
> > > 
> > > 
> > > 		
> > >
> ____________________________________________________
> > > Sell on Yahoo! Auctions - no fees. Bid on great
> > > items.  
> > > http://auctions.yahoo.com/
> > > 
> > > 
> > 
> > 
> > 
> > 		
> >
> ____________________________________________________
> > Sell on Yahoo! Auctions - no fees. Bid on great
> items.  
> > http://auctions.yahoo.com/
> 
> 



		
____________________________________________________
Sell on Yahoo! Auctions  no fees. Bid on great items.  
http://auctions.yahoo.com/

Current Thread