如何通过 XQuery 获取所有元素及其属性的列表 [英] How do I get a list of all elements and their attributes via XQuery
问题描述
我对 XQuery 很陌生,我正在尝试获取所有元素和所有属性的列表.它应该是这样的:
element1 @attributex, @attribue y, ...
元素 2 @attribute x, @attribute y, ...
元素 3 @attribute x, @attribute y, ...
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.
提前致谢,埃莉诺
推荐答案
local-name() 函数将单个节点作为参数,而不是一系列节点.要将相同的函数应用于序列中的每个节点,请使用!"运算符://*!local-name()
.
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()
.
string-join() 函数接受两个参数,一个字符串列表和一个分隔符.您正在尝试传递两个字符串列表.你想要
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()), ',')
当然,您可能还想使用 distinct-values() 删除重复列表,并将元素与属性名称区分开来,或者将属性名称与它们出现的元素相关联.这完全是可能的.但为此,您必须提出更精确的问题.
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.
这篇关于如何通过 XQuery 获取所有元素及其属性的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!