在xquery 3.1中,我正在将XML文件(包含最初以SQL表中保存的数据(转换为另一种XML文件格式。通常,这很简单,但是对于一个特定的元素,我想复制text()
以及任何child
节点及其各自的内容为新元素。例如,我想转换以下XML:
<table tablename="collections">
<record id="1">
<field fieldname="id">1</field>
<field fieldname="title">Quisque elementum cursus nunc non aliquam</field>
<field fieldname="desc">Quisque elementum cursus nunc non aliquam;
also known under the title <i>Lorem ipsum dolor sit amet</i>.<br/> The
same author compiled a large encyclopedia <i>Liber de natura
rerum,</i> synthesizing much knowledge from his period.</field>
<field fieldname="author">Thomas de Cantimpré</field>
</record>
</table>
进入:
<list xml:id="collections">
<item n="1">
<list>
<item type="title">Quisque elementum cursus nunc non aliquam</item>
<item type="desc">Quisque elementum cursus nunc non aliquam;
also known under the title <i>Lorem ipsum dolor sit amet</i>.<br/> The
same author compiled a large encyclopedia <i>Liber de natura
rerum,</i> synthesizing much knowledge from his period.</item>
<item type="author">Thomas de Cantimpré</item>
</list>
</item>
</list>
通常,其中大部分并没有提出问题。但是,我在元素<field fieldname="desc">
中获取text()
和所有child
节点的问题感到困惑。XPATH中的解决方案使我陷入困境。
事先感谢您的任何建议。
您对"所有内容"的口头描述将是xpath术语中的所有子节点,而文本节点当然也是一个元素的子节点,例如子元素是子节点。要选择容器节点的所有子节点,您只需要在容器节点的上下文中需要node()
。/table/record/field[@fieldname="desc"]/node()
选择输入样本的field fieldname="title"
元素的所有子节点。