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

Re: Possible MSXML 3 / 4 bug: using a for-each on xsl

Subject: Re: Possible MSXML 3 / 4 bug: using a for-each on xsl:variable / tre e-frag is misbehaving
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 14 May 2002 15:00:25 +0100
xsl tree
Hi Jeff,

> Here's what I'm seeing: Within an xsl:for-each element, where the
> select is made against an xsl:variable tree-frag, xpath statements
> are failing to nodes outside of the given for-each's context.

I think you're seeing correct behaviour here. Within the xsl:for-each:

>          <xsl:for-each select="msxsl:node-set($items)/item">
>             <using_msxsl_node-set child-id="{@id}">
>                <xsl:copy-of select="/xml/an-element"/>
>             </using_msxsl_node-set>
>          </xsl:for-each>

the current node is an item element within the node set derived from
the $items node set. The path /xml/an-element is resolved relative to
the current node -- the / means the root node of the node tree in
which the item resides.

The $items result tree fragment is generated with:

>          <xsl:variable name="items" >
>             <xsl:copy-of select="//item"/>
>          </xsl:variable>

which means that the result tree fragment is a root node with a number
of item elements as children. The result tree fragment doesn't contain
any xml elements or an-element elements, so it shouldn't surprise you
that it doesn't return anything.

I think that perhaps you're expecting that / always gives you the root
node of the source of the transformation? That's not the case -- it
gives you the root node of the node tree of which the context node is
a part.

So to achieve what you want, store the root node of the source of the
transformation in a (global) variable:

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

and then use that as the basis of your path within the xsl:for-each:

  <xsl:for-each select="msxsl:node-set($items)/item">
    <using_msxsl_node-set child-id="{@id}">
      <xsl:copy-of select="$source/xml/an-element"/>
    </using_msxsl_node-set>
  </xsl:for-each>

---
  
In XPath 2.0, there's a new function called input() that returns the
node(s) that invoked the transformation. So in XSLT 2.0, you could do:

  <xsl:variable name="items">
    <xsl:copy-of select="//item" />
  </xsl:variable>
  <xsl:for-each select="$items/item">
    <using_temporary_tree child-id="{@id}">
      <xsl:copy-of select="input()/xml/an-element" />
    </using_temporary_tree>
  </xsl:for-each>
  
Cheers,

Jeni

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


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.