Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2014, 04:24 AM
Registered User
 
Join Date: Feb 2014
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Error 'Cannot select a node here' in for-each with global variables

Hello,

My stylesheet failed to compile for this error: XPTY0020: Cannot select a node here: the context item is an atomic value at this line:

Code:
<xsl:when test="$event[substring-before(start/datetime, 'T')=$dateJour]">
Here is all the code of the template: http://pastebin.com/fmPUHGpR. The error is on line 20.

Yesterday, I search on a lot of websites and forums who says to put a global variable outside the for-each. $event is a global variable. I try to put event/start/datetime in a global variable but I still have the error.

The processor is XSLT 2.0

Could you please help me?
 
Old February 20th, 2014, 05:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Because you didn't supply a complete self-contained stylesheet I had to make modifications to prevent spurious errors. After these modifications I got an error different from yours, but similar: it objected to the line

[code]
<xsl:value-of select="/workfile/query/consultant[@calendar-id=$calendar-id]/@code" />
[code]

saying

Error on line 166 of test.xsl:
XPTY0020: Leading '/' cannot select the root node of the tree containing the context item:
the context item is an atomic value

I fix this by replacing /workfile/query with $query.

I now get a similar error a few lines further on, with the code

Code:
<xsl:value-of select="id" />
I don't know how to fix this, because I don't know where you are expecting to find the "id" element. So I deleted the line, and it now compiles OK.

It seems to me that you haven't really understood the XSLT concept of the context item. When you do a for-each, or apply-templates, each node or item you are processing becomes the context item. When you use a path expression, it is relative to the context item. When you do for-each select="tokenize(...)", then inside the for-each the context item is a string, and you can't use a path expression that is relative to a string (it has to be a node). The solution is to bind a variable to the relevant node outside the for-each, and use that variable at the start of your paths. It doesn't have to be a global variable, of course.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 20th, 2014, 06:24 AM
Registered User
 
Join Date: Feb 2014
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hello,

I am using XSLT since one week and see your name in a lot of websites and forums so i'm very glad to have an answer of you.

I was blocked because when I put the line
Code:
<xsl:when test="$event[substring-before(start/datetime, 'T')=$dateJour]">
in comment, the stylesheet compiles.

So now, I replace all the /workfile/query by the $query and put in comment the lines
Code:
<xsl:value-of select="id" />
And now, it works fine and I have the good result.

Thank you very much!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Global variables and for-each patrickero XSLT 5 March 11th, 2010 03:29 PM
global variables Komila ASP.NET 3.5 Basics 2 October 31st, 2008 01:14 PM
Global Variables in C# su C# 2 December 11th, 2006 06:18 PM
Global variables zarol Beginning PHP 3 May 10th, 2005 06:17 AM
Global variables ps124 ASP.NET 1.0 and 1.1 Basics 6 April 6th, 2004 02:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.