[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: copy XML and add attributes to ancestors of given

Subject: Re: copy XML and add attributes to ancestors of given element
From: Rick Taylor <taylor@xxxxxxxx>
Date: Wed, 12 Mar 2003 12:23:05 -0700
xml add attribute
Mac,

Its probably easier to test from the top down rather than the bottom up. This is not the most efficient but it works.

<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>

    <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>


At 10:12 AM 3/12/03 -0800, you wrote:


(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


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



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.