XQuery:创建具有给定名称的新元素? [英] XQuery: Create a new element with a given name?
问题描述
我有如下数据:
<td>USERID</td>
<td>NAME</td>
<td>RATING</td>
我想把它变成:
<userid></userid>
<name></name>
<rating></rating>
我该怎么做?
推荐答案
使用 计算的元素构造函数 为每个节点的
元素.小写
值生成一个元素>td
Use a computed element constructor to generate an element with the lower-case
value of the text
nodes for each of the td
elements.
一个计算元素构造函数创建一个元素节点,允许两者节点的名称和内容待计算.
A computed element constructor creates an element node, allowing both the name and the content of the node to be computed.
就示例而言,假设您的 XML 位于名为 foo.xml 的文件中,您可以执行以下操作:
For the sake of the example, assuming that your XML is in a file called foo.xml, you could do something like this:
<doc>
{
for $name in doc('foo.xml')//td/text()
return element {lower-case($name)} {''}
}
</doc>
产生这个:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<userid/>
<name/>
<rating/>
</doc>
您还可以将 lower-case()
函数作为 XPATH 表达式的一部分而不是元素构造函数的一部分进行计算,如下所示:
You could also evaluate the lower-case()
function as part of the XPATH expression instead of the element constructor, like this:
<doc>
{
for $name in doc('foo.xml')//td/text()/lower-case(.)
return element {$name} {''}
}
</doc>
这篇关于XQuery:创建具有给定名称的新元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!