how to change the XML structure using XQuery

basex, xquery

Solution

3 XQuery functions, `substring-before`, `substring-after` and `tokenize` are used to get the required output.

`substring-before` is used to get the Name.

Similarly, the `substring-after` is used to get the Job portion.

Then the `tokenize` function, is used to split the Jobs.

let $data :=
  <E>
    <Employee>AAA@A#B#C#D</Employee>
    <Employee>BBB@A#B#C#D</Employee>
    <Employee>CCC@A#B#C#D</Employee>
    <Employee>DDD@A#B#C#D</Employee>
  </E>


for $x in $data/Employee
return 

<Employee>
   {<Name>{substring-before($x,"@")}</Name>}
   {<Jobs>{
   for $tag in tokenize(substring-after($x,"@"),'#')
   return 
     <Job>{$tag}</Job>
   }</Jobs>
}</Employee>

HTH...

Problem

I have a XML file containing Employees Name and the Job done by them. The structure of the XML file is - ``` <Employee>AAA@A#B#C#D</Employee> <Employee>BBB@A#B#C#D</Employee> <Employee>CCC@A#B#C#D</Employee> <Employee>DDD@A#B#C#D</Employee> ``` There are thousands of records and I have to change structure to - ``` <Employee> <Name>AAA</Name> <Jobs> <Job>A</Job> <Job>B</Job> <Job>C</Job> <Job>D</Job> </Jobs> </Employee> ``` How to get this done using XQuery in BaseX ?

Original source