【问题标题】:XPath ancestor get one of attributesXPath 祖先获取属性之一
【发布时间】:2022-11-23 04:31:22
【问题描述】:

我有一个 Python xml 元素 xpath 表达式 ancestor-or-self::*[@foo]/@foo,我需要修改它以获取属性 @foo(如果它存在),否则获取属性 @bar

我尝试使用类似于 [@foo or @bar] 等条件的 or 运算符,但出现表达式错误

【问题讨论】:

  • 在询问 XPath 问题时,您需要提供 minimal reproducible example:(1) 输入 XML。 (2) 您的逻辑,以及试图实现它的 XPath 表达式。 (3) 基于上述#1 的期望输出。 (4) XPath 处理器及其符合的 XPath 标准:1.0、2.0、3.0、3.1 或 4.0。
  • 让我猜猜(因为您没有发布错误),您使用的是 ElementTree?元素树only supports limited xpath 1.0。你可以使用 lxml 吗?
  • @DanielHaley 你完全正确,谢谢,我错过了

标签: xml xpath


【解决方案1】:

XPath 1.0

为了一个正确的XPath_Prefix, 这个 XPath,

  • XPath_Prefix/@*[name()='foo' or name()='bar'][1]

将选择 foo 属性(如果可用);否则,bar 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多