XML:
<Parent id='1' name='Parent_1'>
<Children name='Children'>
<child name='Child_2' id='2'>child2_Parent_1</child>
<child name='Child_4' id='4'>child4_Parent_1</child>
<child name='Child_1' id='3'>child1_Parent_1</child>
<child name='Child_3' id='1'>child3_Parent_1</child>
</Children>
</Parent>
预期产出:
<Parent id='1' name='Parent_1'>
您好我已经提供了一些示例XML。我已尝试使用X Query获取结果,但我无法识别。你能帮助别人吗?
谢谢!
答案 0 :(得分:3)
我会使用计算元素构造函数执行此操作,其内容仅重新填充属性。
let $root := (: the document, copy-pasted below :)
<Parent id='1' name='Parent_1'>
<Children name='Children'>
<child name='Child_2' id='2'>child2_Parent_1</child>
<child name='Child_4' id='4'>child4_Parent_1</child>
<child name='Child_1' id='3'>child1_Parent_1</child>
<child name='Child_3' id='1'>child3_Parent_1</child>
</Children>
</Parent>
return element { node-name($root) } { $root/@* }
其他内容也可以插入此元素中,如下所示:
let $root := (: the document, copy-pasted below :)
<Parent id='1' name='Parent_1'>
<Children name='Children'>
<child name='Child_2' id='2'>child2_Parent_1</child>
<child name='Child_4' id='4'>child4_Parent_1</child>
<child name='Child_1' id='3'>child1_Parent_1</child>
<child name='Child_3' id='1'>child3_Parent_1</child>
</Children>
</Parent>
return element { node-name($root) } {
$root/@*,
<foo/>,
<bar/>
}