1

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.

My XML looks like this:

<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>

I am looking to get the values of all key attributes (so 'resourcename','foo', etc) ; not the value of the <entry> nodes. There is no way in advance for me to know how many entries there will be, nor what the contents will be.

I have tried the following:

//@* -> gives all attributes, not only the key
//entries/@* -> returns nothing
//entry@* -> returns nothing
//@key -> returns only the first result
//entries/key[*] -> returns nothing
//entry@key -> returns nothing
//entry@key=* -> returns nothing

I probably tried others as well, but these are what I can remember. If it makes any difference: the XPath is executed by the 'XML Webhook' task in Xebialabs' XLRelease.

Tijmen
  • 542
  • 1
  • 6
  • 29
  • 3
    `//@key` is the correct way to get **all** the values, so its not an XPath issue but a tool you're using. Try `//entry/@key` as well – JaSON Sep 29 '20 at 12:06
  • Thanks @JaSON. `//entry/@key` only returns the first value, so apparently the tool is just being a nuisance. Thanks for your help anyway, and I'll see what I can do about the tool. If you want to make your comment the answer, I'd be happy to accept it. – Tijmen Sep 29 '20 at 12:17
  • I reached out to the company that produces the tool I'm using. Apparently, this is by design: the tool is only able to retrieve one result. So I guess I'm off to find another approach. Thanks anyway for the help. – Tijmen Sep 30 '20 at 07:46

1 Answers1

3

@JaSON has already provided you with two viable XPaths in a comment1:

  • //@key will select all key attributes in the document.
  • //entry/@key will select all key attributes of entry elements in the document.

You've commented that //entry/@key only returns the first value and chalked it up to tool/library noncompliance. Realize that there are library APIs that have different calls for returning the first selected item versus returning all selected items.

Here are explanations for each of the attempts you made:

  • //@* selects all attributes in the document.
  • //entries/@* selects nothing because entries has no attributes.
  • //entry@* is syntactically incorrect.
  • //@key selects all key attributes in the document, but see API note above.
  • //entries/key[*] selects all key child elements (of entries elements) with at least one child element
  • //entry@key is syntactically incorrect.
  • //entry@key=* is syntactically incorrect.

1 Will upvote his answer if he posts one.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I reached out to the company that produces the tool I'm using. Apparently, this is by design: the tool is only able to retrieve one result. So I guess I'm off to find another approach. Thanks anyway for the help. – Tijmen Sep 30 '20 at 07:46
  • 1
    I have accepted your answer, but if Jason decides to turn his comment into an answer, I will accept his, considering he was first. – Tijmen Sep 30 '20 at 14:38