首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写此XPath表达式的更好方法

编写此XPath表达式的更好方法
EN

Stack Overflow用户
提问于 2014-01-06 03:12:34
回答 2查看 119关注 0票数 1

是否有更简单的方法来编写这个XPath表达式,特别是最后一行。我正在从tshark中搜索一个XML转储,寻找具有特定属性的某个包。这是我的XPath表达式

代码语言:javascript
复制
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"]])

这是一个计数的数据包,

代码语言:javascript
复制
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的简化版本。请注意,字段标记可能在多个级别内,这就是我为此使用.//的原因。

代码语言:javascript
复制
<?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困住了,因为这是我目前使用的工具所支持的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-06 05:40:45

以下几点似乎有效,对我来说更有意义:

代码语言:javascript
复制
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"]
    ]
])

基本上,第三个标准是:

  • 使用的字段后代
    • “Inferface”的“显示”属性
    • 带有“选项类型: 18”的“显示”属性的子字段
    • 带有"Interface-ID: AVC-0123456789“属性的子字段

您还可以将其写成如下:

代码语言:javascript
复制
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必须满足所有三个缩进条件。

票数 2
EN

Stack Overflow用户

发布于 2014-01-06 04:11:30

这对你有用吗?

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20942375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档