如何在xQuery中获得没有子节点的节点? [英] How to get node without children in xQuery?
问题描述
因此,我实际上有两个要加入的元素节点.我希望顶层节点保持不变,但子节点将被那些交叉引用替换.
So I have two nodes of elements that I'm essentially trying to join. I want the top level node to stay the same but the child nodes to be replaced by those cross referenced.
给出:
<stuff>
<item foo="foo" boo="1"/>
<item foo="bar" boo="2" />
<item foo="baz" boo="3"/>
<item foo="blah boo="4""/>
</stuff>
<list a="1" b="2">
<foo>bar</foo>
<foo>baz</foo>
</list>
我想遍历列表"并交叉引用内容"中的元素以得到此结果:
I want to loop through "list" and cross reference elements in "stuff" for this result:
<list a="1" b="2">
<item foo="bar" boo="2" />
<item foo="baz" boo="3"/>
</list>
我想这样做,而不必知道列表"上可能有哪些属性.换句话说,我不想像
I want to do this without having to know about what attributes might be on "list". In other words I don't want to have to explicitly call them out like
attribute a { $list/@a }, attribute b { $list/@b }
推荐答案
使用:
$list1/item[@foo = $list2/item/@foo]
$list1/item[@foo = $list2/item/@foo]
这将选择$list1
中所有<item>
元素,这些元素的foo
属性值等于$ list2中<item>
元素之一的foo
属性.
This selects all <item>
elements in $list1
the value of whose foo
attribute is equal to the foo
attribute of one of the <item>
elements in $list2.
要复制<list>
元素的所有属性,请执行以下操作:
In order to copy all attributes of the <list>
element, do something like this:
for $attr in /whateverIsthePathLeadingToList/list/@*
return
attibute {name($attr)} {$attr}
这篇关于如何在xQuery中获得没有子节点的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!