Prabu
Prabu

Reputation: 3728

Why is my XPath selecting nothing?

My XML file

<classifications>
  <classification sequence="1">
    <classification-scheme office="" scheme="CS" />
    <section>G</section>
    <class>01</class>
    <subclass>R</subclass>
    <main-group>33</main-group>
    <subgroup>365</subgroup>
    <classification-value>I</classification-value>
  </classification>
  <classification sequence="2">
    <classification-scheme office="" scheme="CS" />
    <section>G</section>
    <class>01</class>
    <subclass>R</subclass>
    <main-group>33</main-group>
    <subgroup>3415</subgroup>
    <classification-value>A</classification-value>
  </classification>
  <classification sequence="1">
    <classification-scheme office="US" scheme="UC" />
    <classification-symbol>324/300</classification-symbol>
  </classification>
  <classification sequence="2">
    <classification-scheme office="US" scheme="UC" />
    <classification-symbol>324/307</classification-symbol>
  </classification>          
</classifications>

I want to parse the value with following condition required all the classification-symbol element value along with condition office="US"

I tried with below XPath,

NodeList usClassification = (NodeList)xPath.compile("//classifications//classification//classification-scheme[@office=\"US\"]//classification-symbol//text()").evaluate(xmlDocument, XPathConstants.NODESET);

but I'm getting an empty result set,

System.out.println(usClassification.getLength()); //its becomes zero

Upvotes: 2

Views: 63

Answers (2)

kjhughes
kjhughes

Reputation: 111591

This XPath (written on two lines to ease readability),

/classifications/classification[classification-scheme/@office='US']
                /classification-symbol/text()

will select the classification-symbol text of the classification elements with classification-scheme @office attribute value equal to US:

324/300
324/307

as requested.

Upvotes: 3

alecxe
alecxe

Reputation: 473873

classification-symbol is not a child of classification-scheme - they are siblings. Use following-sibling axis to get from "scheme" to "symbol" instead:

//classifications/classification/classification-scheme[@office=\"US\"]/following-sibling::classification-symbol/text()

Upvotes: 3

Related Questions