aspose file tools*
The moose likes XML and Related Technologies and the fly likes xpath exp - conventions or just a matter of style ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xpath exp - conventions or just a matter of style ?" Watch "xpath exp - conventions or just a matter of style ?" New topic
Author

xpath exp - conventions or just a matter of style ?

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 09, 2001
Posts: 1180
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


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 09, 2001
Posts: 1180
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 03, 2000
Posts: 5040
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xpath exp - conventions or just a matter of style ?