This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: XML to XML transformation




> -----Original Message-----
> From: owner-xsl-list@mulberrytech.com
> [mailto:owner-xsl-list@mulberrytech.com]On Behalf Of Evyatar_Kafkafi
> Sent: Wednesday, November 22, 2000 12:47 PM
> To: xsl-list@mulberrytech.com
> 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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]