eleonore.shellstrop
eleonore.shellstrop

Reputation: 11

How to get a list of associated elements and attributes (without duplicates) via XQuery


I want to specify my original question (linked below) on how to obtain a list of all elements and their attributes.
What I am looking for is this:

So far, I have tried this:

for $x in collection("XYZ")
let $att := local-name(//@*)
let $ele := local-name(//*)
let $eleatt := string-join($ele, $att)
return $eleatt

which I modified, after reading a helpful comment by @michaelhkay, to this:

for $x in collection("XYZ")
    return distinct-values(string-join(($x//*!local-name(), $x//@*!local-name()), ', 
'))

However, so far, the elements and attributes are not associated and I am also not sure if all distinct values are gone since I still see some twice (however, they could be used with a variety of elements).

I appreciate any help!
Thanks in advance,
Eleonore


Original question: How do I get a list of all elements and their attributes via XQuery

Upvotes: 0

Views: 49

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167561

If you want to eliminate duplicate elements by element name I would use

for $el in collection("XYZ")//*
group by $name := node-name($el)
return ($name, $el[1]/@*!('@' || node-name())) => string-join(' ')

https://xqueryfiddle.liberty-development.net/6qVSgeZ/1

Note however, if you group elements that way by their name, you will not distinguish e.g. <foo att1="value"/> and <foo att2="value"/>, i.e. you would only get the output for one of the foo elements. So how/where you want to eliminate duplicates is not quite clear in that context.

Upvotes: 0

Related Questions