xquery to convert attributes to tags

不羁的心 提交于 2019-12-21 20:48:35

问题


I am learning on Xquery. I have this tag in my XML document.

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

May I know how to create something like the following with 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>

Currently I could only use the static way to do it, like:

$tag := $x/@a and then return it with {$tag

Kindly advise. Thank you very much.


回答1:


This 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)
                 }
       }

Output:

<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>


来源:https://stackoverflow.com/questions/4223926/xquery-to-convert-attributes-to-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!