Reputation: 2239
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.
Upvotes: 3
Views: 2763
Reputation:
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>
Upvotes: 2