所有属性值的 XPath,而不仅仅是第一个?

XPath for all attribute values, not just first?

Edit: the issue in my case was caused by a tool that doesn't fully implement the XPath standard. My attempt with //key@ should have worked (see comments & answer), and the reason it doesn't work is that the tool only shows the first result.

我的 XML 看起来像这样:

<Document id="someIdhere" token="123-456-789" created-by="john_doe" created-at="2020-05-27T10:04:28.244+0000" last-modified-by="jane_doe" last-modified-at="2020-07-30T09:27:59.440+0000">
<somedata/>
<somemoredata/>
<entries>
    <entry key="resourceName">resourceLocation</entry>
    <entry key="foo">bar</entry>
    <entry key="somekey">somevalue</entry>
    <entry key="keyname">keyvalue</entry>
    <entry key="keyname1">value1</entry>
</entries>
<encryptedEntries/>
</Document>

我希望获得所有 key 属性的值(因此 'resourcename'、'foo' 等);不是 <entry> 节点的值。我无法提前知道会有多少条目,也不知道内容是什么。

我试过以下方法:

//@* -> 给出所有属性,而不仅仅是 key
//entries/@* -> returns 无
//entry@* -> returns 无
//@key -> returns 只有第一个结果
//entries/key[*] -> returns 无
//entry@key -> returns 无
//entry@key=* -> returns 无

我可能也尝试过其他的,但这些是我能记住的。如果有任何区别:XPath 由 Xebialabs 的 XLRelease 中的 'XML Webhook' 任务执行。

@JaSON 已经为您提供了两个可行的 XPath 1:

  • //@key 将 select 文档中的所有 key 属性。
  • //entry/@key 将 select 文档中 entry 个元素的所有 key 属性。

您评论说 //entry/@key 只有 returns 第一个值,并将其记为 tool/library 不合规。 意识到有库 API 对返回第一个 selected 项和返回所有 selected 项有不同的调用。

以下是对您所做的每项尝试的解释:

  • //@* select文档中的所有属性。
  • //entries/@* select没什么,因为 entries 没有属性。
  • //entry@* 语法不正确。
  • //@key select 文档中的所有 key 属性,但请参阅上面的 API 注释。
  • //entries/key[*] selects 所有 key 个子元素(entries 个元素)至少有一个子元素
  • //entry@key 语法不正确。
  • //entry@key=* 语法不正确。

1 如果他发帖,他的回答会被点赞。