Subject: Re: [xsl] copy XML and add attributes to ancestors of given element From: Rick Taylor <taylor@xxxxxxxx> Date: Wed, 12 Mar 2003 12:23:05 -0700 |
<xsl:template match="/"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="*[descendant-or-self::*/@task='1']"> <xsl:element name="{name(.)}"> <xsl:for-each select="@*"> <xsl:attribute name="task"> <xsl:value-of select="'1'"/> </xsl:attribute> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates/> </xsl:element>
<xsl:template match="*"> <xsl:element name="{name(.)}"> <xsl:for-each select="@*"> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates/> </xsl:element> </xsl:template>
<xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template>
(I apologize, I just sent this out without replacing some used Subject line. I'm resending this with an appropriate subject. Sorry!)
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.
Can anyone please help?
Thanks so much- Mac
XML ====================== ======================
<desktop id="desktop"> <testSuite currentTask="1"/> <panel id="main"> <panel id="toolbar"> <button id="toolbar.back" task="1">Back</button> <button id="toolbar.forward"></button> <button id="toolbar.home">Show All</button> <button id="toolbar.importFromCamera"></button> <button id="toolbar.organize">Organize</button> <button id="toolbar.fix">Fix</button> <button id="toolbar.create">Create</button> <button id="toolbar.share"></button> <button id="toolbar.order">Order Online</button> <button id="print_tb"></button> <button id="toolbar.slideshow">Slideshow</button> <button id="toolbar.slideshowOptions"></button> <button id="toolbar.calendar">Calendar</button> <button id="toolbar.calendarOptions"></button> <button id="howto_tb">Quick Guide</button> <button id="toolbar.adobe"></button> <QFrame id="v rule"/> <QFrame id="v rule"/> </panel> </panel> </desktop>
XSL ====================== ======================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <root> <xsl:apply-templates /> </root> </xsl:template>
<xsl:template match="*">
<xsl:if test="@task='1'"> <xsl:call-template name="ancest" /> </xsl:if>
<xsl:if test="not(@task='1')"> <xsl:copy-of select="." /> </xsl:if>
</xsl:template>
<xsl:template name="ancest">
<xsl:for-each select="ancestor::*[1]"> <xsl:element name="elementName"> <xsl:value-of select="name()" /> <xsl:attribute name="task"> <xsl:value-of select="1.1" /> </xsl:attribute> </xsl:element> </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Rick Taylor XML Developer PPDM Association
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Looping on a set value while , Alex Dragowski | Thread | RE: [xsl] copy XML and add attribut, cknell |
Re: [xsl] cdata(), Mike Brown | Date | RE: [xsl] copy XML and add attribut, cknell |
Month |