如何使用Xquery将相同的两个XML元素与另一个元素进行比较

时间:2021-07-16 16:14:33

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:

我是Xquery的初学者,我有一个xml代码,我想在其中将两个不同的元素与另一个元素进行比较。如果这两个元素总是存在在一起,那么我想打印它们的值。为了更清楚,我在下面创建了一个示例:

The XML code :

XML代码:

<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.

从上面的例子中,我需要打印Ellizabith和Sam,因为它们存在于Title 1和Titlte 2中。另外我想打印Sam和Ryan,因为它们存在于Title 2和Title 3中使用Xquery。

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

那有什么办法吗?我没有找到任何资源来帮助我做到这一点。

1 个解决方案

#1


0  

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>

#1


0  

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>