0
votes

I am trying to replace partial data of an element with empty string in xquery. I have ticket details xml and email to remove xml with me. Now i need to form an xquery which would modify the ticket details xml by removing the email ids that are there in the element EmailIdToDelete available in second xml from the element EmailId available in first xml and give me the expected output. The xquery version used is not latest. I guess it is very old. Need to check that.

Could someone please help me with this?

Ticket Details xml:

<TicketDetails>
<EmailInformation>
<EmailId>[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];[email protected]</EmailId>
</EmailInformation>
</TicketDetails>

Email to remove xml:

<Details>
<EmailDetails>
<EmailIdToDelete>[email protected]</EmailIdToDelete>
<EmailIdToDelete>[email protected]</EmailIdToDelete>
<EmailIdToDelete>[email protected]</EmailIdToDelete>
</EmailDetails>
</Details>

Expected output:

<TicketDetails>
<EmailInformation>
<EmailId>[email protected];[email protected];[email protected];[email protected];[email protected];</EmailId>
</EmailInformation>
</TicketDetails>
1

1 Answers

0
votes

You could tokenize on ; and then simply compare and only select the values without a match in the second document:

<EmailId>{string-join(tokenize(TicketDetails/EmailInformation/EmailId, ';')[not(. = $remove/Details/EmailDetails/EmailIdToDelete)], ';')}</EmailId>

where $remove is the second document e.g. let $remove := doc('remove.xml') or an external parameter:

declare variable $remove as document-node() external := document {
<Details>
<EmailDetails>
<EmailIdToDelete>[email protected]</EmailIdToDelete>
<EmailIdToDelete>[email protected]</EmailIdToDelete>
<EmailIdToDelete>[email protected]</EmailIdToDelete>
</EmailDetails>
</Details>    
};


<TicketDetails>
<EmailInformation>
<EmailId>{string-join(tokenize(TicketDetails/EmailInformation/EmailId, ';')[not(. = $remove/Details/EmailDetails/EmailIdToDelete)], ';')}</EmailId>
</EmailInformation>
</TicketDetails>

https://xqueryfiddle.liberty-development.net/gWmuPs2