Re: [xsl] Subtree Transformation

Subject: Re: [xsl] Subtree Transformation
From: Abel Braaksma <>
Date: Thu, 15 Mar 2007 13:04:02 +0100
Garvin Riensche wrote:

I am wondering if it is possible to change a subtree of an XML tree whose structure is not always the same.

well, that is precisely what xslt is all about, and it's good at it ;)

If the input would always look like this I would write a stylesheet that looks like the following:

<xsl:stylesheet match="/">

this is not a good idea, use template matching.

I need to copy everything and add some additional "<c>" tags. I dont't know how to do that becase with xsl:copy-of I can copy the whole tree but it can not be changed and if I iterate trough the tree with xsl:copy every tag is immideately closed. It would be nice if someone could help.

XSLT does not work with tags, it works with nodes (subtle but important difference). An element node is always closed, there is no such thing as an open node. When serialized, this results in a pair opening and closing tags, always.

This must be the single most frequently asked question on this list. What you need is <xsl:copy /> and not <xsl:copy-of /> (shallow copy versus deep copy) and make a modified identity template. Like so:

<xsl:template match="/>
  <xsl:apply-templates select="a" />

<xsl:template match="node() | @*" >
     <xsl:apply-templates select="node() | @*" />

<xsl:template match="c">
<!-- first copy the original 'c' node and all attributes + descendants -->
<xsl:apply-templates select="node() | @*" />

 <!-- second, make additional 'c' nodes -->
 <c id="2" />
 <c id="3" />

I advice you to look up a tutorial book (Jeni Tennison's come to mind) or an online tutorial that helps you with understanding template matching, where you declare the rules (you define what outcome you want) and the processor does the math for you based on your input. In this case, the less general match for 'c' has precedence and will be called instead of the general match 'node()' (which does nothing more than shallow copy).

Happy coding!

-- Abel Braaksma

Current Thread