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
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
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.
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.
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”
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:
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 =)