Solved

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

  • 3 September 2019
  • 2 replies
  • 43 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.

icon

Best answer by takashi 4 September 2019, 02:30

View original

2 replies

Userlevel 2
Badge +17

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']

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

Reply