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 September 5th, 2016, 11:54 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Result of // Use is Unexpected

I have a node that is present in two places in my input XML, called "elementA".
The first instance has the value "aaa" and the second instance has the value "bbb".

If I set a variable called "thisVar" to "//elementA[1]" I would expect it to fetch the first elementA node that it finds (since //elementA returns two nodes, and the [1] predicate states I want node 1).

If I then use value-of select="$thisVar", it outputs the two elementA values concatenated, i.e. "aaa bbb".
There are also two nodes in $thisVar, both have the above value.

However, if I set the variable to simply be "//elementA" and then when calling the variable in the value-of, I use the predicate there instead, it works as expected.
I.e. using value-of select="$thisVar[1]" gets me a single node with the value "aaa".

I'm intrigued as to why the first solution doesn't produce the same result as the second solution.

Any info appreciated, thanks.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old September 5th, 2016, 11:58 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well, see https://www.w3.org/TR/xpath20/#abbrev, //elementA[1] is the same as /descendant-or-self::node()/elementA[1]. You might want descendant::elementA[1] or (//elementA)[1]
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 5th, 2016, 12:31 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, this is a well-known gotcha and is well-documented, including in the XPath 1.0 spec itself:

NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 6th, 2016, 07:04 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Lightbulb

Thanks guys.
So the //elementA[1] expression is actually saying, get all elementA nodes that are the first child of their relevant parent, rather than doing the // first to get all nodes, and then applying the predicate?

Makes sense now I've read the documentation. Thanks for the assistance.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find item in one result in another result mphare XSLT 3 October 28th, 2011 10:51 AM
Unexpected error ramkris84 ASP.NET 1.0 and 1.1 Professional 2 April 19th, 2008 04:48 AM
GridView and SQL More than one result = one result DarkForce ASP.NET 2.0 Basics 0 July 20th, 2007 04:29 AM
XSLT: unexpected result saurabh0 XSLT 4 November 2nd, 2004 01:21 PM
Unexpected result from the query tsimsha Classic ASP Databases 1 August 23rd, 2004 03:50 AM





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