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

copy XML and add attributes to ancestors of given elem

Subject: copy XML and add attributes to ancestors of given element
From: "Mac Martine" <email@xxxxxxxxxxxxxxxx>
Date: Wed, 12 Mar 2003 10:12:34 -0800
javascript add attribute to element
(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


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.