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

> -----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="."/>

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="/">
		<!-- this copies the attributes(if any) of the root element -->
		<copy-of select="@*"/>

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

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">
		<!-- this copies across elementA's attributes -->
		<xsl:copy-of select="@*"/>

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
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:attribute name="newAttribute">newValue</xsl:attribute>

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:

Current Thread