Skip to main content
Solved

Find duplicates in xml doc and apply condition in it using XQuery

  • September 3, 2019
  • 2 replies
  • 291 views

I am trying to find duplicates in my xml data and from that I would like to apply condition.

 

 

here is my xml doc

 

 

<results>
    <book>
        <id>1</id> <status> published</status>
    </book>
    <book>
        <id>1</id> <status> not published</status>
    </book>
    <book>
        <id>2</id> <status> sold</status>
    </book>
</results>

From the above xml, I want to find book id where id's are repeated and the status is published then return

 

.
<book>  <id>1</id> <status> published</status> </book>

using XQuery  . 

Could you please help me on this. Thanks in advance.

Best answer by takashi

Hi @vvoona, this is a possible expression, assuming that the variable $doc stores the source XML document.

for $id in distinct-values($doc//id/text())
where 1 < count($doc//id[text() = $id])
return $doc//book[data(id) = $id and normalize-space(data(status)) = 'published']
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

takashi
Celebrity
  • 7843 replies
  • Best Answer
  • September 4, 2019

Hi @vvoona, this is a possible expression, assuming that the variable $doc stores the source XML document.

for $id in distinct-values($doc//id/text())
where 1 < count($doc//id[text() = $id])
return $doc//book[data(id) = $id and normalize-space(data(status)) = 'published']

  • Author
  • 1 reply
  • September 4, 2019

Hi @vvoona, this is a possible expression, assuming that the variable $doc stores the source XML document.

for $id in distinct-values($doc//id/text())
where 1 < count($doc//id[text() = $id])
return $doc//book[data(id) = $id and normalize-space(data(status)) = 'published']

Hello @takashi , it worked for me, you saved my time. Thank you so much :)