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 August 2nd, 2010, 07:51 AM
Authorized User
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default How to access the document node if context item is an atomic value?

Hi,

I do have a snippet like this:
Code:
<xsl:for-each select="('option1', 'option2')">
  <xsl:value-of select="/root/to/some/element"/>
</xsl:for-each>
The, example per se is obviously pretty useless but perfectly illustrates my problem. The error I get is the following:
Code:
XPTY0020: Leading '/' cannot select the root node of the tree containing the context item: the context item is an atomic value
I do understand why this error occurs and what it's telling me but I don't know a way around it. I tried using a variable like this:
Code:
<xsl:variable name="helper" as="element()+">
  <item>option1</item><item>option2</item>
</xsl:variable>
Iterating over that variable instead of my atomic sequence did work, but accessing "/root/to/some/element" obviously failed:
Code:
net.sf.saxon.trans.XPathException: The root of the tree containing the context item is not a document node
(Yeah I assume the last try was pretty desperate ;))

Is there a way to access the document node in my loop?

Thanks in advance,
Florian

P.S.: The only way I found was putting / into a variable before entering the loop, but that's somewhat ugly imo.

Last edited by apollo13; August 2nd, 2010 at 08:01 AM..
 
Old August 2nd, 2010, 07:58 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Create a variable
Code:
<xsl:variable name="doc1" select="/"/>
outside of the for-each, then inside of the for-each you can access
Code:
<xsl:value-of select="$doc1/root/to/some/element"/>
With your current code you get the error as the context item (defined by the select expression of the for-each) is not a node so using a path expression like "/" does not make sense as it needs a context node, not an atomic value like a string.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
apollo13 (August 2nd, 2010)
 
Old August 2nd, 2010, 08:03 AM
Authorized User
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi,

yeah that's what I am currently using (just updated the post, but you were faster ;)). But in my opinion using a variable here is somewhat ugly, so if there are other ways I am all ears.

Cheers,
Florian
 
Old August 2nd, 2010, 08:23 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

An XPath like "/" needs a context node thus when you have a context item that is not a node you need a variable to store a reference to the document you want to access. The same problem can also occur in XSLT 1.0 when you process nodes from different documents, you need a variable too in that case. It might look ugly to you but I don't think there is solution that is shorter or more elegant.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 2nd, 2010, 08:27 AM
Authorized User
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Guess I'll live with it then ;) Thx for your answers.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Return to source context after document('') jkmyoung XSLT 2 June 30th, 2007 03:04 AM
Position of a node outside current context QuickSilver002 XSLT 2 April 19th, 2007 02:07 PM
context-item returns...? maxtoroq XSLT 2 October 22nd, 2006 09:29 PM
Item Missing in Transformed Document raohara XSLT 2 July 27th, 2006 12:09 AM
Understanding context node in templates Birger XSLT 3 August 31st, 2004 04:21 AM





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