xquery将属性转换为标签

时间:2010-11-19 10:03:26

标签: xml xquery

我正在学习Xquery。我的XML文档中有这个标记。

<element a="1" b="2" c="3" name="testgroupID">198</element>
<element a="11" b="12" c="13" name="testgroupverifyID" binary="hidden"/>

我是否知道如何使用xquery创建类似下面的内容?

<mytags>
    <a>1</a>
    <b>2</b>
    <c>3</c>
    <name>testgroupID</name>
    <value>198</value>
</mytags>
<mytags>
    <a>11</a>
    <b>12</b>
    <c>13</c>
    <name>testgroupverifyID</name>
    <binary>hidden</binary>
</mytags>

目前我只能使用静态方式来执行此操作,例如:

$ tag:= $ x / @ a 然后使用{$ tag

返回它

请告知。非常感谢你。

1 个答案:

答案 0 :(得分:2)

这个XQuery:

for $elem in /root/element
return element mytags {
          for $child in $elem/(@*|text())
          return element {if ($child instance of attribute())
                          then name($child)
                          else 'value'} {
                    string($child)
                 }
       }

输出:

<mytags>
    <a>1</a>
    <b>2</b>
    <c>3</c>
    <name>testgroupID</name>
    <value>198</value>
</mytags>
<mytags>
    <a>11</a>
    <b>12</b>
    <c>13</c>
    <name>testgroupverifyID</name>
    <binary>hidden</binary>
</mytags>
相关问题