RE: [xsl] copy XML and add attributes to ancestors of given element

Subject: RE: [xsl] copy XML and add attributes to ancestors of given element
From: Americo Albuquerque <aalbuquerque@xxxxxxxxxxxxxxxx>
Date: Wed, 12 Mar 2003 19:53:26 -0000
Hi Mac.


> -----Mensagem original-----
> De: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] Em nome de Mac Martine
> Enviada: quarta-feira, 12 de Marco de 2003 18:13
> Para: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Assunto: [xsl] copy XML and add attributes to ancestors of 
> given element
> 
> 
> 
(...)
> Hello-
>  	I am trying to simply duplicate an XML tree with the 
> addition of adding an attribute to all the ancestors of a 
> given element.
> 
> 	In the example provided I am trying to copy all 
> elements, but when I find an element where @task='1', I want 
> to give all of its ancestors an attribute called 'task' as 
> well. My current code is below.
>  

Try this:

 <xsl:template match="*">
  <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates select="descendant::*[@task=1]/@task"/>
   <xsl:apply-templates select="*"/>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="@task">
 <xsl:attribute name="task">1.1</xsl:attribute>
 </xsl:template>

The <xsl:apply-templates select="descendant::*[@task=1]/@task"/> will do
nothing if ther is no descendant::*[@task=1]/@task

You could also use <xsl:apply-templates
select="descendant::*/@task[.=1]"/> but I find the first easy to read
and understand

Hope that this helps you


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread