XPath to get names of attributes

I have XML elements that looks like this:

<myElement attribute1="value1" attribute2="value2" attribute3="value3"/>

I want to query all attribute values and have the attribute names as column names in the output table. Now, the number of attributes and their names are static so I could just do n queries and hardcode the names, but that’s boring :upside_down_face:

I know that /dns:myElement/@* with multi-column gets me all attributes, but generic column names.
The function name() yields attribute names, but unfortunately isn’t vector friendly: It will only return the name of the first node in a given node-set, so name(/dns:myElement/@*) is not multi-column compatible.

If what I want is not possible, can I at least rely on /dns:myElement/@* being sorted (because it seems to be alphabetically sorted)? Thank you :evergreen_tree:

Expected Output:
Unbenannt

Hi @Thyme , I would need a proper XML sample with multiple myElement to be able to produce your output, but you could look into converting the XML to JSON and then JSON to Table and then with some String Manipulation/Column Rename for tidying up.

Something like this:
image

1 Like

Tested it with the real data, this works. Thank you!

If anyone wants to Node Golf the full problem:
Get only the attribute values whose name starts with a specific substring (say “abc”). Those are numeric. Some of the excluded attributes are of type string if that’s important.
I’ll obviously do that with Column Filter and String to Number nodes after the JSON to Table node, but I’m curious whether or not it can be done without the JSON detour.

Hi @Thyme , it certainly can be done without JSON, but it will not be as direct and straight forward.

Filtering via attribute names can be done like this:

/dns:myElement/@*[starts-with(name(), "abc")]

I considered opening a feature request to enable the XPath Node to assign dynamic column names, only to find that it should be possible already (see screenshot). The problem is, I can’t get it to work. Using the default “name” yields only empty strings for Single Cell and Multi Row options, and fails for the Multi Column Option:

Execute failed: javax.xml.xpath.XPathExpressionException: Number of values differs from number of column names.

Any variants with local-name, slashes, brackets, dots and any combinations of those didn’t work either. Maybe the crux of the matter is that the expression for the column name is appended at the end of the expression for the value. Or I don’t see it.
PS: I found a typo in the node description: In the settings block it says “Eidt XPath” :grin:

Hi @Thyme , basically you would need an XML To Table node, just like we have a JSON To Table node. It’s essentially the JSON to Table node that’s doing the “magic” here. But I think XML can be much more complex than JSON, so it’s probably a challenge because of this.

Hmm, all I need now is to use the attribute names as column names within the XPath node. I’m still hoping someone finds out how to pull that off. Although I’m not so sure anymore whether it can actually be done. The XPath node uses XPath 1.0 and I’d need 2.0 to do this for dynamic column names:

/dns:myElement/@*[starts-with(name(), "abc")]/name()

this is mentioned here:

This post also mentions that there exists a ticket to upgrade the XPath node (May 2019):

@ipazin Can you add +1 from me? Thank you :smiley:

Right now I’m at 12 nodes with your neat little construct @bruno29a, versus 2 with an XPath-only solution. It’s quick to set up, so I’m good for now. :evergreen_tree:

PS: NodePit has the same typo.

Hi @Thyme , I’ll request at +1 for me too to help bump the priority up.

@ipazin fyi, it’s about this:

1 Like

and to be sure, maybe it’s not the same for the devs: The linked thread is about the JSON Path node, while I want the XPath node to be upgraded. thx =)

I’ve added +1s from both of you on ticket AP-11745, which is about adding XPath 2.0 support :slight_smile:

5 Likes

Yes, that’s why I quoted that line @Thyme , but indeed, it also adds the title of the other thread, which could be confusing.

Thanks @ScottF for adding the +1s and for confirming for which feature :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.