xsl-list
[Top] [All Lists]

copy XML and add attributes to ancestors of given element

2003-03-12 11:12:34

(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