问题
I am quite new to XQuery and I am trying to get a list of all elements and all attributes.
It should look like this:
element1 @attributex, @attribue y, ...
element 2 @attribute x, @attribute y, ...
element 3 @attribute x, @attribute y, ...
I am trying this so far, but the error "Item expected, sequence found":
for $x in collection("XYZ")
let $att := local-name(//@*)
let $ele := local-name(//*)
let $eleatt := string-join($ele, $att)
return $eleatt
I feel like I am turning an easy step into a complicated one. Please help.
Thanks in advance, Eleonore
回答1:
//@* gives you a sequence of attribute nodes, //* a sequence of element nodes. In general to apply a function like local-name() to each item in a sequence, for nodes you have three options:
- Use a final step
/local-name()e.g.//@*/local-name()or//*/local-name() - In XQuery 3.1 use the map operator
!e.g.//@*!local-name() - Use a
for .. returnexpression e.g.for $att in //@* return local-name($att)
回答2:
The local-name() function takes a single node as its argument, not a sequence of nodes. To apply the same function to every node in a sequence, using the "!" operator: //*!local-name().
The string-join() function takes two arguments, a list of strings, and a separator. You're trying to pass two lists of strings. You want
string-join((//*!local-name(), //@*!local-name()), ',')
Of course you might also want to de-duplicate the list using distinct-values(), and to distinguish element from attribute names, or to associate attribute names with the element they appear on. That's all eminently possible. But for that, you'll have to ask a more precise question.
来源:https://stackoverflow.com/questions/65956786/how-do-i-get-a-list-of-all-elements-and-their-attributes-via-xquery