如何选择具有特定值的孙子节点的节点 [英] How to select the node with grand-grandchild node that has particular value
问题描述
我只想获取其中值为123abc"的元素 A.我两个都试过,但都失败了.
Hi I just want to get the Element A with value of "123abc" in it. I have tried both but failed.
//包/A[A/B/C/.= 123abc]
//Package/A[A/B/C/. = 123abc]
//包/A[包含(A/B/C,123abc)]
//Package/A[contains(A/B/C,123abc)]
我希望它返回:
<A>
<System mtm="8742" os="Windows XP" oslang="en" />
<System mtm="2055" os="Windows XP" oslang="jp" />
<A>
<B>
<C>123abc</C>
<C>789</C>
<C>567</C>
</B>
</A>
</A>
用于运行查询的示例 xml:
Sample xml to run query on:
<?xml version="1.0" encoding="UTF-8"?>
<Database version="300">
<Package>
<A>
<System mtm="8742" os="Windows XP" oslang="en" />
<System mtm="2055" os="Windows XP" oslang="jp" />
<A>
<B>
<C>123abc</C>
<C>789</C>
<C>567</C>
</B>
</A>
</A>
</Package>
<Package>
<A>
<System mtm="8742" os="Windows XP" oslang="en" />
<System mtm="2055" os="Windows XP" oslang="jp" />
<A>
<B>
<C>efg123</C>
<C>789</C>
<C>567</C>
</B>
</A>
</A>
</Package>
</Database>
您可以在这里测试您的答案:http://www.freeformatter.com/xpath-tester.html#ad-output
You can test your answer here: http://www.freeformatter.com/xpath-tester.html#ad-output
添加了我正在尝试处理的实际 xml,但它会不会工作,因为特殊字符会被转义吗?
Added the actual xml i am trying to process, but won't work could it be special characters escaped?
我正在尝试处理的实际 xml 如下,我已经尝试过
actual xml i am trying to process is below, i have tried
//TableSection/SectionItem[SectionItem/Cell/.= "00-18-E7-17-48-64"]
//TableSection/SectionItem[SectionItem/Cell/. = "00-18-E7-17-48-64"]
//TableSection/SectionItem[contains(SectionItem/Cell,"00-18-E7-17-48-64")]
//TableSection/SectionItem[contains(SectionItem/Cell,"00-18-E7-17-48-64")]
<TableSection name="SNMP Devices" IsTreeFormat="true">
<SectionProperties>
<Column id="1" Name="IP Address" />
<Column id="2" Name="Description" />
</SectionProperties>
<SectionItem>
<Cell columnid="1">
192.168.99.54
</Cell>
<Cell columnid="2">
WMI XScan
</Cell>
<SectionItem>
<Cell columnid="1">
ScanType
</Cell>
<Cell columnid="2">
WMIScan
</Cell>
</SectionItem>
<SectionItem>
<Cell columnid="1">
Device description
</Cell>
<Cell columnid="2">
WMI dscription
</Cell>
</SectionItem>
<SectionItem>
<Cell columnid="1">
MACAddress
</Cell>
<Cell columnid="2">
00-18-E7-17-48-64
</Cell>
</SectionItem>
</SectionItem>
<SectionItem>
<Cell columnid="1">
192.168.99.55
</Cell>
<Cell columnid="2">
WMI XScan
</Cell>
<SectionItem>
<Cell columnid="1">
ScanType
</Cell>
<Cell columnid="2">
WMIScan
</Cell>
</SectionItem>
<SectionItem>
<Cell columnid="1">
Device description
</Cell>
<Cell columnid="2">
WMI dscription
</Cell>
</SectionItem>
<SectionItem>
<Cell columnid="1">
MACAddress
</Cell>
<Cell columnid="2">
90-2B-34-64-16-9D
</Cell>
</SectionItem>
</SectionItem>
<SectionItem>
<Cell columnid="1">
192.168.99.107
</Cell>
<Cell columnid="2">
VMWare : "navvms08.Crest.local"
</Cell>
<SectionItem>
<Cell columnid="1">
MACAddress
</Cell>
<Cell columnid="2">
00-07-E9-0D-05-C5
</Cell>
</SectionItem>
<SectionItem>
<Cell columnid="1">
Device identifier
</Cell>
<Cell columnid="2">
1.3.6.1.4.1.6876.4.1
</Cell>
</SectionItem>
<SectionItem>
<Cell columnid="1">
Device name
</Cell>
<Cell columnid="2">
"navvms08.Crest.local"
</Cell>
</SectionItem>
<SectionItem>
<Cell columnid="1">
Device description
</Cell>
<Cell columnid="2">
"VMware ESXi 5.5.0 build-1623387 VMware, Inc. x86_64"
</Cell>
</SectionItem>
</SectionItem>
</TableSection>
推荐答案
在原始示例中,您需要引用比较字符串.这两项工作:
In the original sample you need to quote your comparison string. Both of these work:
//Package/A[A/B/C/. = "123abc"]
//Package/A[contains(A/B/C,"123abc")]
针对您运行的实际 XML 的查询应该是:
The query for the actual XML you're running against should be:
//TableSection/SectionItem[SectionItem/Cell[contains(.,"00-18-E7-17-48-64")]]
<小时>
问题:
//TableSection/SectionItem[SectionItem/Cell/. = "00-18-E7-17-48-64"]
是文本内容必须完全匹配,而 XML 有前导和尾随空格.
is that the text content must match exactly, while the XML has leading and trailing whitespace.
问题:
//TableSection/SectionItem[contains(SectionItem/Cell,"00-18-E7-17-48-64")]
是 contains()
只查找 //TableSection/SectionItem
中的第一个 SectionItem/Cell
而不是 any SectionItem/Cell
在其中查找文本.
is that contains()
only looks in the first SectionItem/Cell
within //TableSection/SectionItem
instead of any SectionItem/Cell
therein to find the text.
这篇关于如何选择具有特定值的孙子节点的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!