Xpath 路径帮助
我有以下 XML:
<products>
<product>
<name>Flat Panel Monitor</name>
<code>LS123</code>
<attributes>
<attribute>
<group>Color</group>
<values>
<value>Red</value>
<value>Blue</value>
<value>Green</value>
</values>
</attribute>
<attribute>
<group>Warranty</group>
<values>
<value>5 Year</value>
</values>
</attribute>
</attributes>
</product>
</products>
我将使用什么 Xpath 来获取组节点值为“Color”的属性节点的所有值? /product/attributes/attribute/values/value
的标准 Xpath 将返回所有值,包括保修组内的值,但我需要将它们分开。
我想在伪代码中我所说的是获取所有“值”,其中父节点“值”是具有值“颜色”的节点“组”的兄弟节点 - 希望这是可能的吗?
谢谢。
I have the following XML:
<products>
<product>
<name>Flat Panel Monitor</name>
<code>LS123</code>
<attributes>
<attribute>
<group>Color</group>
<values>
<value>Red</value>
<value>Blue</value>
<value>Green</value>
</values>
</attribute>
<attribute>
<group>Warranty</group>
<values>
<value>5 Year</value>
</values>
</attribute>
</attributes>
</product>
</products>
What Xpath would i use to get all value's with the attribute node with the group node value of "Color" ? A standard Xpath of /product/attributes/attribute/values/value
would return all value's including value's from within the Warranty group but i need to keep them separate.
I guess in pseudocode what i'm saying is get all "value" where the parent node "values" is a sibling of the node "group" with the value "Color" - hopefully this is possible?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用方括号来过滤节点,因此:
/product/attributes/attribute[group='Color']/values/value
You need to use square brackets to filter the nodes, thus:
/product/attributes/attribute[group='Color']/values/value
您只需要那些具有
Color
的group
'uncle' 的value
节点,因此请将其作为您已经使用的 xpath 的条件out:这表示要使
value
节点有效,它必须有一个父节点,而父节点的子节点的值为Color
。You require only those
value
nodes that have agroup
'uncle' ofColor
, so put that as a condition on the xpath you have already worked out:This says that for a
value
node to be valid, it must have a parent with a parent with a child with valueColor
.还有另一个选项:
//value[ancestor::attribute[group='Color']]
Yet another option:
//value[ancestor::attribute[group='Color']]