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

6
  • Look at this question Return a string value based on XPATH condition Commented Jun 26, 2012 at 4:55
  • @codewagge, i saw the post . Want i would like to have is ,implementing the condition in a java program. Commented Jun 26, 2012 at 5:02
  • You might get more help if you include that info in the question. Commented Jun 26, 2012 at 5:05
  • Java isn't my area, hopefully the new tag and title will catch the eye of someone who does Java. Commented Jun 26, 2012 at 5: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. Commented Jun 26, 2012 at 5:38

2 Answers 2

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()); 
}
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

Let me know if you check this. In addition to emulate true 'if' clause you can consume only first attribute in the result.
@victor, actualy i am trying to implement this in java .
i tried it, it is throwing an error, " Attribute name "book" associated with an element type "indiv" must be followed by the ' = ' character."
Fix the typo first: '<indiv book author="hamle" />' It has incorrect XML syntax.
I have fixed the typo. How do i implement this in java ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.