aspose file tools*
The moose likes XML and Related Technologies and the fly likes issue in xpath while getting ancestor's sibling 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 "issue in xpath while getting ancestor Watch "issue in xpath while getting ancestor New topic
Author

issue in xpath while getting ancestor's sibling

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
Hi All,

i am again facing one issue in getting ancestor's sibling using xpath, below is my xml.



Where i am -> i am currently at the highlighted position in the above xml which is in green.
Where i need to reach new -> i need to reach at the highlighted position in the abouve xml which is in red.


below is the xpath which i am trying but it is not working.

"/Query/first/second[ancestor::row[third[value[.='dddd']]]/following-sibling::*]/value[.='aaaa']";

Thanks,
Punit
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
oops colors doesn't work in code tags.

anyways,

i am at line 120 and need to reach at line 144
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
please let me know if any other information is require or if my question looks bit confusing?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Let's have a look at your XPath expression:

/Query/first/second[ancestor::row[third[value[.='dddd']]]/following-sibling::*]/value[.='aaaa']

Since you've attached a predicate to "second", that is going to result in a set of "second" element nodes. So you're asking for /Query/first/second elements which satisfy a particular complicated condition, and then find their value children which satisfy another particular condition. But since there aren't any "second" elements which have "value" children, you get an empty nodeset. Regardless of whatever that complicated condition was supposed to do. (And since no "second" element has a "row" element as an ancestor, you probably have that wrong as well.)

I also don't understand why you have a predicate which is looking for "value" elements with 'dddd' as the value. You said

i am currently at the highlighted position in the above xml


Should we take that to mean that the current node in your XSLT processing is that element? You didn't mention XSLT but what are we to make of that statement otherwise?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

And if the current node for your XPath is what you say it is, then why not just this?

ancestor::row//value[.='aaaa']
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
Hi Paul.
Thanks for your reply.
Let me explain you what i am trying to achieve.

so from that xml i want those two values which i highlighted in this format :- dddd:aaaa

so i have written two methods:

In my first method (Working fine) i have written one XPath :

/Query/first/second/row/third/value[.='"+cba+"'] -> the value for cba can be changed, so i am taking it as an string type argument.


In my second method (facing issues) : i am trying to write XPath on top of my first method XPath.


Here are my expectations from my second method.

In my second method XPath it should go in that particular row element nodes for which the value for value tag is matched to cba.
and from that row element nodes i want value from attrID tag or say any other tag.

i know it is quite confusing and complex



Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
if it is confusing, in little words this is what i am trying to achieve.

From my xml whichever value tag <value></value> has text which i am passing to the method with argument cba, From that row block only <row className="" id=""> i want to get the text of value tag <value></value> which has key <key></key> text as value

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

I think I understand. For others who might want to work on this: when you say "From" you don't mean you want to navigate from that location to another location. You mean you want to extract data from that location and then navigate from the root element to another location. Right? If so then why not this?

//value[.='aaaa']
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: issue in xpath while getting ancestor's sibling