xsl-list
[Top] [All Lists]

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

2006-09-13 16:44:56
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(_at_)visualclick(_dot_)de>
To: "XSL List" <XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com>
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--