【问题标题】:Select xml node by attribute name ignoring namespace of that attribute按属性名称选择 xml 节点,忽略该属性的命名空间
【发布时间】:2011-05-23 05:57:51
【问题描述】:

我有一个这样的节点:

<meta name="og:description" content="Here's the content" />

如果名称是“描述”,无论它是否在命名空间中,我都希望能够选择此元素。如果元标记的名称是“og:description”、“description”、“blah:description”等,我需要能够选择它。

我已经看到了 xpath 的资源,这些资源展示了如何在命名空间内进行选择,但不是与命名空间无关。

【问题讨论】:

  • og:name="description" 是命名空间,但 name="og:description" 只是类似于命名空间的内容,不是吗?

标签: xml xpath


【解决方案1】:

使用

//meta[@*[local-name() = 'description']]

这将选择 XML 文档中具有本地名称 "description" 的属性的所有 meta 元素。

根据定义,标准 XPath 函数 local-name() 生成节点的名称,命名空间前缀(如果有的话)从该节点被剥离。

注意:如果 XML 文档的结构是静态已知的,则始终避免使用 // 伪运算符。经常使用// 会导致执行缓慢。

【讨论】:

    【解决方案2】:

    使用 XPath 2 你可以做到:

     /meta[ends-with(@name, 'description')]
    

    对于 XPath 1,我们需要:

     /meta['description' = substring(@name, string-length(@name) - string-length('description') + 1)]
    

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多