我的XML文件爲什麼我的XPath不選擇任何東西?
<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
儘量避免以這種方式濫用「//」。這是一種懶惰的習慣,當你在相同的路徑上執行四次時,它會嚴重傷害到性能,尤其是當源文檔很大時。 –
正確,尤其是因爲'//'(選擇後代)和'/'(選擇直接子元素)之間的功能差異與您的XML無關。 – kjhughes