Re: [xsl] Global location path confusion

Subject: Re: [xsl] Global location path confusion
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 22 Feb 2001 22:06:17 +0000
Hi Jonas,

> Why does the second "abc" get eaten up?
> Why doesn't it result in this?
> <doc>
>   <x title="abc"></x>
>   <x title="abc"></x>
> </doc>

When you match the 'b' element in your source, the following template
gets applied:

  <xsl:template match="b">
     <xsl:param name="content">
     <xsl:apply-templates select="$content"/>

In this template, you create what used to be called a result tree
fragment - a new node set.  This is assigned to the parameter
$content.  The new node set looks like:

+- (root)
   +- (element) a

In other words, it has a root node with a single child node - an 'a'

Then you apply templates to the $content parameter (implicitly turning
the result tree fragment into a node set - only legal in XSLT 1.1),
you're essentially applying it to this new node tree.  In the template
matching the 'a' element:

  <xsl:template match="a">
    <x title="{/doc/@title}"/>

The value of the 'title' attribute is being set to the value of the
'title' attribute on the 'doc' element that's a child of the root node
(in the document that the current node comes from).

The 'document' that the current node (the 'a' element that you're
matching) comes from is this little node tree that you've created. The
root node of that node tree doesn't have a 'doc' element child, let
alone one with a 'title' attribute, so evaluating that location path
gives an empty string.

If you wanted to get the value of the 'title' attribute on the 'doc'
element of the *original source tree*, then you need to store the root
node of the source tree in a global variable:

  <xsl:variable name="source" select="/" />

You can then get that value with:


whatever the current node is, whatever document (or document fragment)
it's in.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread