Re: [xsl] Using absolute path when context item is an atomic value xslt 2.0

Subject: Re: [xsl] Using absolute path when context item is an atomic value xslt 2.0
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 18 Oct 2005 21:18:01 +0100
> <xsl:template match="node">
>   <xsl:for-each select="1 to 10">
>     <xsl:apply-templates/>
> 
> would apply-templates to <node> 10 times.  This isn't possible because
> the context node is an atomic value and of course it makes no sense to
> apply-templates that, so it's an error.
> 
> As Mike said in another reply, they thought about having a context
> document that would remain when the context item was an atomic value -
> which makes most sense to me - but it was dropped so the alternative
> is to recreate that with variables.
> 
> The concept of the context "node" being an atomic value adds quite a
> bit to the learning curve...
> 

I think that the current way (without a separate context node) works out
best on practice.  People will expect that apply-templates in the above
will work, but then they'll expect that

 <xsl:template match="node">
   <xsl:for-each select="1 to 10">
     <xsl:value-of select="."/>

would produce (as it does) 1 2 3 4 5 6 7 8 9 10.

So something has to give, if "." was defined  so that it wasn't always
the node used as the base for relative path expressions, then other
"well known" xslt 1 idioms start to break as well. Whatever they do
something has to be a bit unintuitive, anyway gives Dave P something to
update his faq:-) 

In xslt1 there is (almost?) always a context node, so you expect
relative path expressions to always work (even if they select nothing)
in xslt2 you just have to get used to them failing sometimes. As well as
failing in the above when the current item is atomic, they fail in the
top level of function bodies (and a few other places) where the context
item is explicitly undefined. (The current node isn't preserved through a
function call, if the function body needs it you need to pass . as a
parameter: only built-in functions can have an implicit parameter of
. if passed with no arguments (mainly for xpath 1 compatibility)

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