【发布时间】:2014-01-06 03:12:34
【问题描述】:
有没有更简单的方法来编写这个 XPath 表达式,特别是最后一行。我正在从 tshark 搜索 XML 转储,以查找具有某些属性的某个数据包。这是我的 XPath 表达式
count(//packet[
proto/@name="dhcpv6"
and .//field/@showname="Message type: Solicit (1)"
and .//field/@show="Link-layer address: 30:00:01:dc:d9:82"
and .//field[@show="Interface-Id"]/field[@show="option type: 18"]/../field[@show="Interface-ID: AVC-0123456789"]])
这是获取数据包的计数
1) Contain a child of <proto name="dhcpv6">
2) Contain a descendant of <field showname="Message type: Solicit (1)/">
3) Contain a descendant of <field show="Link-layer address: 30:00:01:dc:d9:82"/>
4) Contain a descendant of
<field show="Interface-Id">
<field show="option type: 18"/>
<field show="Interface-ID: AVC-0123456789"/>
</field>
我不喜欢的一点是它找到“选项类型:18”,然后返回父级并查找“接口 ID:AVC-0123456789”。有没有办法用'and'语句来写这个?它有效,但有点令人困惑和难以理解的 IMO。
这是 XML 的简化版本。请注意,字段标签可能在多个级别内,这就是我使用 .// 的原因。
<?xml version="1.0"?>
<packet>
<proto name="dhcpv6">
<field showname="Message type: Solicit (1)"/>
<field show="Link-layer address: 30:00:01:dc:d9:82"/>
<field show="Interface-Id">
<field show="option type: 18"/>
<field show="Interface-ID: AVC-0123456789"/>
</field>
</proto>
</packet>
顺便说一句,我坚持使用 XPath 1.0,因为这是我目前使用的工具所支持的。
【问题讨论】: