Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xpath exp - conventions or just a matter of style ?

 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some reading I have seen expressions like this:
Say, for example select all titles with "The Good, The Bad and The Ugly...
title[.="The Good, The Bad and The Ugly"]
title["The Good, The Bad and The Ugly"]
Is this just a matter of style or is there more that I am missing ?
In the limited exposure that I have, these two expressions seem to do the same thing. For some reason, I prefer the second option...
Thanks for any input.
- madhav
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhav,
. (dot) refers to the context node. In your example, both expression mean the same.
There is not standard way of writing this expression, though most people actually use the second form (without the ".=" ).
Cheers
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These two predicates mean two different things.
[.="The Good, The Bad and The Ugly"] means that the title element will be chosen when the title element contains this string as its data.
The predicate ["The Good, The Bad and The Ugly"] will _always_ evaluate to true since this string "The Good, The Bad and The Ugly" is not empty. An empty string will make the predicate return false.
Cheers,
Dan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan,
You do not seem to have used XPath extensively, even if you seem to have some time reading the spec
If you look at the spec carefully, indeed, there is no mention of this abbreviated form. However most parsers will understand it.
In XPath, leaving out the ".=" in a predicate is quite common as it simplifies writing long expressions.
Cheers
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be kind enough to check it with the following code -

xsl -

XML Spy 4.4 and xalan output the following -

Cheers,
Dan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan,
Damn ! You were right
Thanks for your clear demonstration, and appologises for my sarcasm
After seing your reply, I went straight to my favourite XSLT transformer to prove you wrong, and well, I have to admit that I'm lacking some practise in that area
Madhav,
Sorry for the confusion. I've been confusing this with the abbreviated form element[1] (== element[position()=1]).
Cheers
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Dan.
Appreciate your example. Although I made a few
changes to understand your point. I was interested in selecting an element based on its content. Say I want to select all elements of a specific type. To understand this I used your code and this is what I have:

Result:

Apparently, as you said, the rule A is selected for all elements. Rule B is selected only for that element whose content matches 'desk'.
(For the desk, rule A is not selected since it has its own rule).
WOW! So its not a convention, its a valid selection rule and that's what I wanted to understand.
Thanks.
- madhav............used XML Spy.
[ January 17, 2003: Message edited by: Madhav Lakkapragada ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic