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

别来无恙 提交于 2021-02-11 12:32:10

问题



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:

  1. Use a final step /local-name() e.g. //@*/local-name() or //*/local-name()
  2. In XQuery 3.1 use the map operator ! e.g. //@*!local-name()
  3. Use a for .. return expression 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!