0
votes

I have the data as below manner.

<Status>Active Leave Terminated</Status>
<date>05/06/2014 09/10/2014 01/10/2015</date>

I want to get the data as in the below manner.

<status>Active</Status>
<date>05/06/2014</date>

<status>Leave</Status>
<date>09/10/2014</date>

<status>Terminated</Status>
<date>01/10/2015</date>

please help me on the query, to retrieve the data as specified above.

2
Please be aware that you should always show what you already tried! I answered this time, but the community in general does not appreciate such questions. Nevertheless, welcome to SO! - dirkk

2 Answers

3
votes

Well, you have a string and want to split it at the whitestapces. That's what tokenize() is for and \s is a whitespace. To get the corresponding date you can get the current position in the for loop using at. Together it looks something like this (note that I assume that the input data is the current context item):

let $dates := tokenize(date, "\s+")
for $status at $pos in tokenize(Status, "\s+")
return (
  <status>{$status}</status>,
  <date>{$dates[$pos]}</date>
)
1
votes

You did not indicate whether your data is on the file system or already loaded into MarkLogic. It's also not clear if this is something you need to do once on a small set of data or on an on-going basis with a lot of data.

If it's on the file system, you can transform it as it is being loaded. For instance, MarkLogic Content Pump can apply a transformation during load.

If you have already loaded the content and you want to transform it in place, you can use Corb2.

If you have a small amount of data, then you can just loop across it using Query Console.

Regardless of how you apply the transformation code, dirkk's answer shows how you need to change it. If you are updating content already in your database, you'll xdmp:node-delete() the original Status and date elements and xdmp:node-insert-child() the new ones.