下面是 XML
<?xml version="1.0" encoding="UTF-8"?>
<library>
<object>book</object>
<bookname>
<value>testbook</value>
<author>
<value>ABCD</value>
<category>
<value>story</value>
<price>
<dollars>200</dollars>
</price>
</category>
</author>
<author>
<value>EFGH</value>
<category>
<value>fiction</value>
<price>
<dollars>300</dollars>
</price>
</category>
</author>
</bookname>
</library>
我需要 xpath 表达式来获得以下输出
<?xml version="1.0" encoding="UTF-8"?>
<library>
<object>book</object>
<bookname>
<value>testbook</value>
<author>
<value>ABCD</value>
<category>
<value>story</value>
<price>
<dollars>200</dollars>
</price>
</category>
</author>
</bookname>
</library>
但是当我应用下面的 xpath 表达式时,我将整个输入 xml 作为转换后的输出。相反,我只需要匹配作者/值='ABCD'的父节点+子节点(如上所示)
<xsl:copy-of select="/library/object[text()='book']/../bookname/value[text()='testbook']/../author/value[text()='ABCD']/../../.."/>
请帮助我使用正确的 xpath 表达式以获得所需的输出。
我正在使用一个 java 程序来评估 xpath 表达式以获得我想要的 XML 输出。所以我需要一个 xpath 表达式。下面是我的java代码
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("books.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/library/object[text()='book']/../bookname/value[text()='testbook']/../author/value[text()='ABCD']/../../..");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
请帮助我在Java 或 xslt中提供正确的解决方案