• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

XPath - match nodes on two levels

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I want to use xpath to match the second companySetup node in the XML below (if and only if two criterias matches). Let's say I want the companySetup with the attribute companyID="1" and the sub node /companySetup/canteen/canteenID with the text value = 50.

I tried to execute the xpath below, but it returns the first companySetup node which has the canteenID text value = 20.

Any help would be appreciated.

The XML looks like:
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like this.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

Simple and easy
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I get the list of all canteenIDs (50,51,52) which belongs to companyID="1"?

I have tried with the expressions:
String expression = "/invoice/companySetup[@companyID='1']/canteen/canteenID"

and

String expression = "/invoice/companySetup[@companyID='1' and canteen/canteenID]"

but it didn't work.

 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I get the list of all canteenIDs (50,51,52) which belongs to companyID="1"?


I have tried with the expressions:
String expression = "/invoice/companySetup[@companyID='1']/canteen/canteenID"


This path seems just fine. Only, if you continue to use selectSingleNode() as shown in your first post, you won't possibly get them as such. You can certainly use selectNodes() (read the documentation) as a sort of natural continuity.

But you should notice from the documentation that selectSingleNode() and selectNodes() and many others are deprecated for some time. You should look for the use of XPathExpression and Filter etc... to do it in the future. It's up to you.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic