0

I have a sample xml file which looks like the one below:

<Root>
    <SubOne>
       <book author="george" />
       <indiv name="abc" />
       <indiv name="khh" />
       <indiv name="ioo" />
    </SubOne>
    <SubTwo>
       <indiv book author="hamle" />
       <indiv name="kok"/>
       <indiv name="ppp" />
    </SubTow>

</Root>

Is there a way in XPATH to check , if author="george" select /Root/SubOne/indiv/@name , or if the author="hamle", select /Root/SubTwo/indiv/@name. Any help is appreciated

codewaggle
  • 4,893
  • 2
  • 32
  • 48
Parameswar
  • 1,951
  • 9
  • 34
  • 57
  • Look at this question [Return a string value based on XPATH condition](http://stackoverflow.com/questions/7045151/return-a-string-value-based-on-xpath-condition) – codewaggle Jun 26 '12 at 04:55
  • @codewagge, i saw the post . Want i would like to have is ,implementing the condition in a java program. – Parameswar Jun 26 '12 at 05:02
  • You might get more help if you include that info in the question. – codewaggle Jun 26 '12 at 05:05
  • Java isn't my area, hopefully the new tag and title will catch the eye of someone who does Java. – codewaggle Jun 26 '12 at 05:09
  • It is much better to google a how-to than to ask for a specific piece of code on SO. People will help you find solutions but they won't provide coding service. – user845279 Jun 26 '12 at 05:38
  • @parameswar: Refer to my **[answer](http://stackoverflow.com/a/11233547/500725)** – Siva Charan Jun 27 '12 at 19:38

2 Answers2

1

First I noticed that your XML is not valid.

Assume that your XML is as follows.

XML:

<Root>
    <SubOne>
        <book author="george"/>
        <indiv name="abc"/>
        <indiv name="khh"/>
        <indiv name="ioo"/>
    </SubOne>
    <SubTwo>
        <book author="hamle"/>
        <indiv name="kok"/>
        <indiv name="ppp"/>
    </SubTwo>
</Root>

XPATH:

For @author='george', use this XPATH:

//*[book[@author='george']]/indiv

For @author='hamle', use this XPATH:

//*[book[@author='hamle']]/indiv

For both XPATH, to print the name use @name

JAVA:

Using Java, you need to implement this way for both xpath's

For @author='george',

String xpath = "//*[book[@author='george']]/indiv";
NodeList nl = (NodeList) xpath.evaluate(xpath, xml, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getAttributes().getNamedItem("name").getNodeValue()); 
}

For @author='hamle',

String xpath = "//*[book[@author='hamle']]/indiv";
NodeList nl = (NodeList) xpath.evaluate(xpath, xml, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getAttributes().getNamedItem("name").getNodeValue()); 
}
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
0

I guess you have a typo in node /Root/SubTwo/indiv. This should work (I haven't check)

/Root/SubOne/book[@author='george']/following-sibling::1/@name | /Root/SubTwo/book[@author='hamle']/following-sibling::1/@name

It uses union so thats not exactly what you are looking for.

Viktor Stolbin
  • 2,899
  • 4
  • 32
  • 53