使用 xpath 和条件查找 XML 个元素
Finding XML elements using xpath and conditions
我有一个 XML 文档,我需要为其提取一些文本,我正在尝试定义一个 xpath 表达式,它会根据 ancestor/sibling 节点的值找到该元素.
举个例子更简单...在下面的 XML 中,对于每个 <feature>
节点,我只想获取 <content>
元素的值,前提是<name>
元素是“正”:
<root>
<feature id="1c4">
<head>
<name>positive</name>
</head>
<values>
<value id="qwqwq">
<content>Content-yes</content>
</value>
</values>
</feature>
<feature id="1c5">
<head>
<name>negative</name>
</head>
<values>
<value id="wtrwt">
<content>Target-no</content>
</value>
</values>
</feature>
</root>
我可以轻松找到所有元素(包括我不想要的元素)://root/feature/values/value/content
如果内容值为“正”,我可以找到 <name>
个元素://root/feature/head/name[text() = "positive"]
但是,只有当 //root/feature/head/name[text() = "positive"]
为真时,我如何才能将两者结合起来找到 //root/feature/values/value/content
的值?
我尝试在 <feature>
节点级别组合表达式,但这似乎不是有效的 xpath 表达式://root/feature[/head/name[text() = "positive" and /values/value/content]
在此先感谢您的帮助!
F
试试这个...
/root/feature[head/name='positive']/values/value/content
我有一个 XML 文档,我需要为其提取一些文本,我正在尝试定义一个 xpath 表达式,它会根据 ancestor/sibling 节点的值找到该元素.
举个例子更简单...在下面的 XML 中,对于每个 <feature>
节点,我只想获取 <content>
元素的值,前提是<name>
元素是“正”:
<root>
<feature id="1c4">
<head>
<name>positive</name>
</head>
<values>
<value id="qwqwq">
<content>Content-yes</content>
</value>
</values>
</feature>
<feature id="1c5">
<head>
<name>negative</name>
</head>
<values>
<value id="wtrwt">
<content>Target-no</content>
</value>
</values>
</feature>
</root>
我可以轻松找到所有元素(包括我不想要的元素)://root/feature/values/value/content
如果内容值为“正”,我可以找到 <name>
个元素://root/feature/head/name[text() = "positive"]
但是,只有当 //root/feature/head/name[text() = "positive"]
为真时,我如何才能将两者结合起来找到 //root/feature/values/value/content
的值?
我尝试在 <feature>
节点级别组合表达式,但这似乎不是有效的 xpath 表达式://root/feature[/head/name[text() = "positive" and /values/value/content]
在此先感谢您的帮助!
F
试试这个...
/root/feature[head/name='positive']/values/value/content