java 如何使用 XPath 按 TextContent 过滤元素?通过轴获取父级?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3019644/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use XPath to filter elements by TextContent? get parent by axis?
提问by Michael Mao
I've found a similar question on SO, however, that seems not exactly what I wanna achieve:
我在 SO 上发现了一个类似的问题,但是,这似乎不是我想要实现的:
Say, this is a sample XML file:
比如说,这是一个示例 XML 文件:
<root>
    <item>
        <id isInStock="true">10001</id>
        <category>Loose Balloon</category>
    </item>
    <item>
        <id isInStock="true">10001</id>
        <category>Bouquet Balloon</category>
    </item>
    <item>
        <id isInStock="true">10001</id>
        <category>Loose Balloon</category>
    </item>
</root>
If I wanna get a "filtered" subset of the item elements from this XML, how could I use an XPath expression to directly address that?
如果我想从这个 XML 中获得一个“过滤”的 item 元素子集,我如何使用 XPath 表达式来直接解决这个问题?
XPathExpression expr = xpath.compile("/root/item/category/text()");
I now know this would evaluate to be the collection of all the TextContent from the categories, however, that means I have to use a collection to store the values, then iterate, then go back to grab other related info such as the item id again.
我现在知道这将评估为类别中所有 TextContent 的集合,但是,这意味着我必须使用集合来存储值,然后迭代,然后再返回获取其他相关信息,例如项目 ID .
Another question is : how could I refer to the parent node properly?
另一个问题是:如何正确引用父节点?
Say, this xpath expression would get me the collection of all the id nodes, right? But what I want is the collection of item nodes:
比如说,这个 xpath 表达式会让我得到所有 id 节点的集合,对吧?但我想要的是项目节点的集合:
XPathExpression expr = xpath.compile("/root/item/id[@isInStock='true']");
I know I should use the "parent" axis to refer to that, but I just cannot make it right...
我知道我应该使用“父”轴来指代它,但我就是做对了……
Is there a better way of doing this sort of thing? Learning the w3cschools tutorials now...
有没有更好的方法来做这种事情?现在学习 w3cschools 教程...
Sorry I am new to XPath in Java, and thanks a lot in advance.
抱歉,我是 Java 中 XPath 的新手,非常感谢。
回答by Dimitre Novatchev
If I wanna get a "filtered" subset of the item elements from this XML, how could I use an XPath expression to directly address that?
如果我想从这个 XML 中获得一个“过滤”的 item 元素子集,我如何使用 XPath 表达式来直接解决这个问题?
An example XPath expression:
XPath 表达式示例:
/*/item[id/@isInStock='true']/category/text()
/*/item[id/@isInStock='true']/category/text()
This XPath expression selects all text-node children of all <category>elements of all <item>elements the isInStockattribute of whose idchild has a value of 'true'and (the idelements) that are children of the top element of the XML document.
此 XPath 表达式选择所有<category>元素的所有<item>元素的所有文本节点子元素,isInStock其id子元素的属性值为'true'和(id元素),这些元素是 XML 文档顶部元素的子元素。
Another question is : how could I refer to the parent node properly?
另一个问题是:如何正确引用父节点?
Use:
使用:
parent::node()
parent::node()
or simply
或者干脆
..
..
回答by Hirosh Wickramasuriya
Java.
爪哇。
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
Following XPath expression can be used to filter element by text contents.
以下 XPath 表达式可用于按文本内容过滤元素。
**String xPathString = "/root/item/category[text()='Bouquet Balloon']";** 
XPath xPath = XPathFactory.newInstance().newXPath();     
XPathExpression xPathExpression = xPath.compile(xPathString);
Get the result evaluated to the a required type,
将结果评估为所需的类型,
Object result = xPathExpression.evaluate(getDrugBankXMLDoc(), XPathConstants.NODE);
Get the parent of the selected Node (import org.w3c.dom.Node;),
获取选中节点的父节点(import org.w3c.dom.Node;),
Node baloon = (Node)result;
if (baloon != null)
{
     baloon = baloon.getParentNode();
}
回答by Matthew Flynn
To select the item, You can continue the path after the predicate to jump back to the parent of the found id(s)
选择item,可以继续谓词后的路径跳回到找到的id的父级(s)
XPathExpression expr = xpath.compile("/root/item/id[@isInStock='true']/../text()");
When you evaluate this, it should return a NodeList containing the filtered itemNodes (and their subtrees), which you can then iterate through.
当您对此进行评估时,它应该返回一个包含已过滤项节点(及其子树)的 NodeList ,然后您可以对其进行迭代。

