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: Global location path confusion


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">
       <a/>
     </xsl:param>
     <xsl:apply-templates select="$content"/>
  </xsl:template>

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'
element.

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}"/>
  </xsl:template>

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:

  $source/doc/@title

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

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 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]