我有下面的XML:XPath表達式不正常
<thoughts>
<thought>
<id>1</id>
<category>Leadership</category>
<what>sometext</what>
<who>sometext</who>
</thought>
<thought>
<id>2</id>
<category>Leadership</category>
<what>sometext</what>
<who>sometext</who>
</thought>
... 100s of category Leadership
<thought>
<id>1</id>
<category>Love</category>
<what>sometext</what>
<who>sometext</who>
</thought>
<thought>
<id>2</id>
<category>Love</category>
<what>sometext</what>
<who>sometext</who>
</thought>
... 100s of category Love
... and so on up to about ten categories
</thoughts>
我想選擇一個思想(什麼)對於給定的ID和類別。我正在用Java來做這件事。我試過如下:
"/thought[id='1']/thought[category='Love']/what/text()"
的Java:
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression expr1 = xPath.compile("/thought[id='1']/thought[category='Love']/what/text()");
Object result1 = expr1.evaluate(doc, XPathConstants.NODESET);
NodeList nodes1 = (NodeList) result1;
我也曾嘗試以下XPathExpressions:
/thoughts/thought[id='1']/thought[category=`Love`]/what/text()
我是新來的XML和XPath。
返回'NULL'。我沒有找到很多幫助的教程。請幫忙。 – harshit
根據http://www.xpathtester.com/test XPath表達式應該沒問題 –
是第一個回答。 – harshit