我的XML文件

<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>

我要用以下条件分析该值
需要所有classification-symbol元素值以及条件office="US"
我尝试使用下面的xpath,
NodeList usClassification = (NodeList)xPath.compile("//classifications//classification//classification-scheme[@office=\"US\"]//classification-symbol//text()").evaluate(xmlDocument, XPathConstants.NODESET);

但我得到的结果是空的,
System.out.println(usClassification.getLength()); //its becomes zero

最佳答案

这个xpath(写在两行上以便于阅读)

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

将选择属性值等于classification-symbolclassification元素的classification-scheme文本:
324/300
324/307

按要求。

10-08 16:24