0

I've a requiremnet to generate dynamic elements and values based on two xmls. How can I achieve using XQUERY.

I have data like in two xmls:

<td>USERID</td>
<td>NAME</td>
<td>RATING</td>

<id>1</id>
<name>Tom</name>
<grade>A</grade>

Expected:

<userid>1</userid>
<name>Tom</name>
<rating>A</rating>
Learner
  • 5
  • 1

1 Answers1

1

The key here is the dynamic element constructor:

let $names  := (<td>USERID</td>, <td>NAME</td>, <td>RATING</td>)
let $values := (<id>1</id>, <name>Tom</name>, <grade>A</grade>)
for $name at $pos in $names
return
  element { fn:lower-case($name) } { fn:data($values[$pos]) }
Florent Georges
  • 2,190
  • 1
  • 15
  • 24
  • Thank you @ Florent Georges for a quick response. Is there a way I can use FLOWR instead of position because if any one of the elements is missing in the values then assignment goes wrong. eg: name element is missing then grade value goes into name. – Learner Sep 20 '22 at 15:15