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

Re: Subtree Transformation

Subject: Re: Subtree Transformation
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 15 Mar 2007 13:04:02 +0100
Re:  Subtree Transformation
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="/">
<a>
  <b>
    <xsl:copy-of="/a/b/c"/>

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>

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

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


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


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!

Cheers,
-- Abel Braaksma

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.