Re: [xslt] Selecting on nodeset in variable from stylesheet



On Sun, 2003-12-14 at 08:31, Pike wrote:
> Hi
> 
> > So I guess my stylesheet should have looked like this:
> >
> > <xsl:variable name="foo"><name>bar</name></xsl:variable>
> > <xsl:value-of select="exslt:node-set($foo)/name"/>
> >
> > Ok, fair enough.  I think it is a particularly ugly thing that you 
> > need to do to get the result you expect.  I assume that will be better 
> > in XSLT 2.0...  ;-)
> 
> I agree. plus, after months I'm still confused as to when a var turns 
> out
> to be a tree frag or a plain var, especially when dragging vars through 
> templates
> and functions ...

The whole node set vs. result tree issue always confuses beginners, and
understandably so.  Here's the basic rule:  An XPath expression inside
of a select attribute gives you a node set (except, of course, when it
gives you a number or something else).  A template always gives you a
result tree fragment.  If you variable looks like this:

<xsl:variable name="foo" select="some/xpath/expression"/>

then you have a node set (unless you have a number of something).  If
your variable looks like this:

<xsl:variable name="foo">
   [... some XSLT template stuff ...]
</xsl:variable>

then you have a result tree fragment.

> in practice, i turn every var i need to approach as a treefrag
> into a node-set right away, eg:
> 
> > <xsl:variable name="foo-src"><name>bar</name></xsl:variable>
> > <xsl:variable name="foo"><xsl:value-of 
> > select="exslt:node-set($foo-src)"/></xsl:variable>
> ....[other code]
> > <xsl:value-of select="$foo/name"/>

As Daniel pointed out, this is a bad idea.  XSLT has some shortcomings,
and you should just learn to work within them, rather than fighting the
language.  Besides the performance penalty and the non-portability that
this introduces, this can lead you to even more confusion.  Consider

<xsl:variable name="foo">
   <xsl:copy-of select="foo"/>
</xsl:variable>
<xsl:variable name="fooNS" select="exslt:node-set($foo)"/>

Now $foo contains a result tree fragment, and $fooNS contains a node
set.  But since $fooNS "comes from" the original document, you might
expect that you could do ancestor selections right up to the root node
of the document.  But you can't.  $fooNS is entirely disconnected from
the original document.  It's a copy that's been coerced into being a
node set.

> btw, the saxon interpreter _does_ treat tree fragments as you would 
> expect them,
> which is probably against the specs, and not portable, but more 
> intuitive.

Then this is a very serious bug.  Sometimes standards aren't as good as
they ought to be.  The correct thing to do is to use the standard (or
don't use it at all) and provide feedback for the next version.  Making
changes in one's implementation just because "it seems better" leads to
serious problems that are hard to rectify later on.  This is exactly why
the Internet sucks so much.

--
Shaun





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]