Re: [xsl] xsl:for-each with atomic values and nested xsl:apply-templates

Subject: Re: [xsl] xsl:for-each with atomic values and nested xsl:apply-templates
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Date: Wed, 13 Sep 2006 17:53:33 -0500
Hi, Christian,

By using tokenize inside a for-each, you've set the context to a string that
has no relationship to your input document.

To fix it, use a variable that contains the root element (I call those
"anchor variables"), thus:

  <xsl:template match="src">
    <xsl:variable name="root" select="/"
    <xsl:for-each select="tokenize( 'a c', '[ ]')">
      <xsl:apply-templates select="$root/doc/*[current() eq string(@id)]" />
    </xsl:for-each>
  </xsl:template>

That way, your apply-templates instruction finds the context you need.

Jay Bryant
Bryant Communication Services

----- Original Message ----- 
From: "Christian Roth" <roth@xxxxxxxxxxxxxx>
To: "XSL List" <XSL-List@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Wednesday, September 13, 2006 5:12 PM
Subject: [xsl] xsl:for-each with atomic values and nested
xsl:apply-templates


> Hi,
>
> trying to figure out what the error with this fragment (XSLT 2) is:
>
>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="2.0">
>
>   <xsl:template match="src">
>     <xsl:for-each select="tokenize( 'a c', '[ ]')">
>       <xsl:apply-templates select="/doc/*[current() eq string(@id)]" />
>     </xsl:for-each>
>   </xsl:template>
>
> </xsl:stylesheet>
>
>
> This gives an error, "Cannot select a node here: the context item is an
> atomic value".
>
> I see that the xsl:for-each iterates over a sequence of atomic values
> (xs:string). So I assume that current() results in an xs:string, which
> should be comparable to the string(@id) expression. The select="..."
> expression does not depend on a context node, as it is an absolute path
> from the document root. xsl:apply-templates results in a sequence and
> therefore should be no problem in the xsl:for-each body. It's certainly
> trivial - I just don't see it right now... :-/
>
> An input for the above would e.g. be:
>
>
> <doc>
>     <src />
>     <elem id="a">A</elem>
>     <elem id="b">B</elem>
>     <elem id="c">C</elem>
>     <elem id="d">D</elem>
> </doc>
>
>
> The desired output would (probably...) be:
>
> AC
>
>
> Regards, Christian

Current Thread