Skip to main content
Solved

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

  • September 3, 2019
  • 2 replies
  • 272 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']
View original
Did this help you find an answer to your question?

2 replies

takashi
Influencer
  • 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
  • September 4, 2019
takashi wrote:

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 :)


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings