Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Xpath - find node

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Let´s say I want to find the page node with the value 'B2' using Xpath. How can we find the page node if we know the parent node value in a single expression. I have tried the following expression but it doesn´t seem to work:

content/pageGroup/page[.='B1']/pageGroup/page[.='B2']

 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Note: I edited your post so that the document was indented in such a way that you can see its structure. When you posted it, the indentation was apparently random. This doesn't help in analyzing it.)

Anyway, now that you can see how the XML is organized, you can see that the <page> element whose string value is B1 doesn't have any <pageGroup> children. So when you ask for its <pageGroup> children, that's where you go wrong. Instead you need to go back to its parent element before looking for <pageGroup> children.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:(Note: I edited your post so that the document was indented in such a way that you can see its structure. When you posted it, the indentation was apparently random. This doesn't help in analyzing it.)

Anyway, now that you can see how the XML is organized, you can see that the <page> element whose string value is B1 doesn't have any <pageGroup> children. So when you ask for its <pageGroup> children, that's where you go wrong. Instead you need to go back to its parent element before looking for <pageGroup> children.


Arrh... yes of course. I didn´t thought about that... Thank you for your quick reply.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic