xsl-list
[Top] [All Lists]

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

2003-03-12 12:53:26
Hi Mac.


-----Mensagem original-----
De: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com 
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] Em nome de 
Mac Martine
Enviada: quarta-feira, 12 de Marco de 2003 18:13
Para: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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::*[(_at_)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::*[(_at_)task=1]/@task"/> will do
nothing if ther is no descendant::*[(_at_)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