Problem whith a tokenize loop

N

Nicolas M

Hi, i've got a problem :

i want to iterate over a list of string created via tokenize(), but i
also want to fetch an attribute value of a node that as an attribute
with the value of my current list item :

<xsl:for-each select="tokenize(my_string)"> <xsl:variable
name="label" select="."/> <xsl:variable name="id"
select="//mynode[@label=$label]/@id"/>
<xsl:value-of select="$id"/>
</xsl:for-each>

But i've got an error form le latest version of SAXON :
"Cannot select a node here: the context item is an atomic value"

Something i did wrong ?

Thanks for your help

Nicolas
 
D

David Carlisle

Nicolas said:
Hi, i've got a problem :

i want to iterate over a list of string created via tokenize(), but i
also want to fetch an attribute value of a node that as an attribute
with the value of my current list item :

<xsl:for-each select="tokenize(my_string)">
<xsl:variable name="label" select="."/>
<xsl:variable name="id" select="//mynode[@label=$label]/@id"/>
<xsl:value-of select="$id"/>
</xsl:for-each>

But i've got an error form le latest version of SAXON :
"Cannot select a node here: the context item is an atomic value"

Something i did wrong ?

Thanks for your help

Nicolas


yes paths starting with // are still relative Xpaths they mean select
from the current document, which means outermost ancestor of teh current
node, but in a context where there is no current node, they error.
you want
<xs:variable name="root" select="/"/>
<xsl:for-each select="tokenize(...)">
select="$root//mynode[....


so that you get back to the original document.

although actually paths of the form //foo[something=zzz] are almost
always better written as a key (as xsl;key is specifically there to
optimise that construct)

<xsl:key name="k" match="mynode" use="@label"/>
Then the whole of the above construct can be written as

<xsl:value-of select="key('k',tokenize(my_string))/@id"/>

(actually it's a bit different from the above in the order ids are
returned, but it probably does what you want.)

David
 
N

Nicolas M

David Carlisle a écrit :
<xs:variable name="root" select="/"/>
<xsl:for-each select="tokenize(...)">
select="$root//mynode[....

Thanks a lot, that does the job,

i will look for xsl:key too

Thanks

Nicolas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top