如何获取 XML 根元素及其属性,但没有内容
How to get the XML root element with its attributes, but without the content
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 获取结果,但无法识别。你能帮助别人吗?
谢谢!
我会使用计算元素构造函数来执行此操作,其内容仅重新填充属性。
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/>
}
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 获取结果,但无法识别。你能帮助别人吗?
谢谢!
我会使用计算元素构造函数来执行此操作,其内容仅重新填充属性。
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/>
}