我有以下 XML:
<ONIXMessage>
<Product>
<RecordReference>9786071502131</RecordReference>
<RecordReference>9786071502131</RecordReference>
</Product>
<Product>
<RecordReference>9786071502131</RecordReference>
</Product>
</ONIXMessage>
以及以下java代码:
Element ONIXmessage = document.getDocumentElement();
products = XPathAPI.selectNodeList(ONIXmessage, "/ONIXMessage/Product");
for(int i = 0;i < products.getLength();i++) {
Node product = products.item(i);
NodeList prova = XPathAPI.selectNodeList(ONIXmessage, "/ONIXMessage/Product/RecordReference");
System.out.println(prova.getLength());
NodeList prova2 = XPathAPI.selectNodeList(product, "/ONIXMessage/Product/RecordReference");
System.out.println(prova2.getLength());
}
此代码返回:3 3 3 3
我认为这段代码应该返回 3 2 3 1 因为“prova”变量包含文档的所有“RecordReference”节点,而“prova2”只包含一个产品节点的特定“RecordReference”节点。
如何使用 XPATH 仅获取特定产品的节点?