Re: [xsl] How to access the root node from within a subtemplate/recursion?

Subject: Re: [xsl] How to access the root node from within a subtemplate/recursion?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 1 Dec 2004 00:44:24 GMT
> Obviously xslt only knows the local tree at the current recursion level.
> Is there a way to access the root node of the whole outer tree?

XSLT has full access to teh entire tree (trees in fact) at all times.

> <xsl:template match="//myelem[@name = 'peter']">
you don't want the // there it isn't doing anything useful the
stylesheet would work the same way with
<xsl:template match="myelem[@name = 'peter']">

     <!-- now I want to jump to the node <myelem name="karl"> with the subnode 'tony'
          but this doesn't work because this node is outside the context/scope -->
     <xsl:with-param name="nametofind" select="//myelem[@name =
          $nametofind]" />

//myelem  will search the whole document for an element of that name, so
unless you have multiple documents (from document() or xx:node-set()
functions) this wil find the element.

If you do have multiple documeents and want to get back to your original
one, put
<xsl:variable name="originaldoc" select="/"/>
at teh top level of your stylesheet (outside any template)
then you can go
     <xsl:with-param name="nametofind" select="$originaldoc//myelem[@name = $nametofind]" />

Note that // can be very expensive/slow and you probably would get great
efficiency gains by using keys. (butget it working before getting it
optimised)

> How do I access from a given subtree the outer whole tree resp. other
> nodes?
if it really is a subtree then just using / will work, if it's a
different tree, use a variable holding the original root node, as
above. 

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread