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

RE: XML to XML transformation

Subject: RE: XML to XML transformation
From: "John Power" <john.power@xxxxxxxxxxxx>
Date: Thu, 23 Nov 2000 10:15:00 -0000
xsl transform modify attribute

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxx]On Behalf Of Evyatar_Kafkafi
> Sent: Wednesday, November 22, 2000 12:47 PM
> To: xsl-list@xxxxxxxxxxxxxxxx
> Subject: XML to XML transformation
>
>
> I'd like to use XSLT to transform an XML document to another XML document.
> To begin with, I tried to write an XSL stylesheet that creates a result
> document that is the same as the source
To directly copy the XML over to the result tree, you do:

<xsl:template match="/">
	<xsl:copy-of select="."/>
</xsl:template>

but this won't allow you to modify anything in the XML.

A more flexible method might be this:

<!-- First of all, copy the root and apply-templates -->
<xsl:template match="/">
	<copy>
		<!-- this copies the attributes(if any) of the root element -->
		<copy-of select="@*"/>
		<xsl:apply-templates/>
	</copy>
</xsl:template>

<!-- This will match any other elements, copy them and their attribute nodes
and apply-templates -->
<xsl:template match="*">
	<copy>
		<!-- this copies the attributes(if any) of the element -->
		<copy-of select="@*"/>
		<xsl:apply-templates/>
	</copy>
</xsl:template>

With this method, your stylesheet will, by default, copy across all XML
element content. But the method also allows you to single out and alter the
processing of individual elements and attributes.
For example, say you want to copy across all your XML except for one element
called "elementA". You want to replace "elementA" with "elementB". You write
the two templates exactly as they are above and also make a template to
match "elementA" specifically.

<xsl:template match="elementA">
	<elementB>
		<!-- this copies across elementA's attributes -->
		<xsl:copy-of select="@*"/>
		<xsl:apply-templates/>
	</elementB>
</xsl:template>

You can alter an element's attributes as well. Generally, all you need to do
is leave out the '<xsl:copy-of select="@*"/>' and make new attributes using
'xsl:attribute'.
Again, for example, to replace the attributes of "elementA" with one
attribute called "newAttribute" which has a value of "newValue":

<xsl:template match="elementA">
	<xsl:copy>
		<xsl:attribute name="newAttribute">newValue</xsl:attribute>
		<xsl:apply-templates/>
	</xsl:copy>
</xsl:template>

If you want to copy SOME attributes as they are or alter the value of an
attribute, you still have to create new attributes. It's just that you name
the attributes after the original and give them a new value.

Hope this helps.
P.S.this method works on Xalan-J.



 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.