redRose
redRose

Reputation: 83

How compare same two XML elements with another element using Xquery

I am a beginner in Xquery and I have an xml code in which I want to compare two different elements with another element. If those two elements always exists together so I want to print the value of them. To make it more clear, I created an example below of what I have so far:

The XML code :

<library>
<Book>
    <title>Title1</title>
    <author>Sam</author>
    <author>Jon</author>
    <author>Ellizabith</author>
    </Book>
<Book>
    <title>Title2</title>
    <author>Ellizabith</author>
    <author>Sam</author>
    <author>Ryan</author>
</Book>
<Book>
    <title>Title3</title>
    <author>Ryan</author>
    <author>Sam</author>
</Book>
</library>

from the above example, I need to print Ellizabith and Sam since they exist in Title 1 and Titlte 2, Also I want to print Sam and Ryan since they exists in Title 2 and Title 3 using Xquery.

So is there any way to do it? I didn't find any resource to help me to do it.

Upvotes: 0

Views: 1332

Answers (1)

Ewout Graswinckel
Ewout Graswinckel

Reputation: 1273

I misinterpreted at first, but here is a new attempt. This will return some duplicates which I might fix later if I have time (fixed now)

for $book in //Book
let $authors := $book/author
for $authorA in $authors, $authorB in ($authors[. gt $authorA])
where ($book/following::Book)[author = $authorA and author = $authorB]
return <result>{($authorA, $authorB)}</result>

Upvotes: 0

Related Questions