是否有更简单的方法来编写这个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”,然后返回到父程序并查找"Interface-ID: AVC-0123456789“。有什么方法可以用'and‘语句来写这个吗?它工作,但有点混乱和读不懂,海事组织。
以下是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困住了,因为这是我目前使用的工具所支持的。
发布于 2014-01-06 05:40:45
以下几点似乎有效,对我来说更有意义:
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"
and field[@show="option type: 18"]
and field[@show="Interface-ID: AVC-0123456789"]
]
])基本上,第三个标准是:
您还可以将其写成如下:
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"]]
])这意味着第三个.//field必须满足所有三个缩进条件。
发布于 2014-01-06 04:11:30
这对你有用吗?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:variable name="count" select="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'] and ./field[@show='option type: 18'] and ./field[@show='Interface-ID: AVC-0123456789']])"/>
<xsl:template match="/">
<div>
Count is: <xsl:value-of select="$count"/>
</div>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/20942375
复制相似问题