1

我试图让评论者对 2010 年之后出版的一本或多本书进行评论。

for $r in doc("review.xml")//Reviews//Review,
    $b in doc("book.xml")//Books//Book
where $b/Title = $r/BookTitle
    and $b/Year > 2010
return {$r/Reviewer}

以下都是 XML 文件。

审查.xml:

<Reviews>
    <Review>
        <ReviewID>R1</ReviewID>
        <BookTitle>B1</BookTitle>
        <Reviewer>AAA</Reviewer>
    </Review>
    <Review>
        <ReviewID>R2</ReviewID>
        <BookTitle>B1</BookTitle>
        <Reviewer>BBB</Reviewer>
    </Review>
    <Review>
        <ReviewID>R3</ReviewID>
        <BookTitle>B2</BookTitle>
        <Reviewer>AAA</Reviewer>
    </Review>
    <Review>
        <ReviewID>R4</ReviewID>
        <BookTitle>B3</BookTitle>
        <Reviewer>AAA</Reviewer>
    </Review>
<Reviews>

书.xml:

<Books>
    <Book>
        <Title>B1</Title>
        <Year>2005</Year>
    </Book>
    <Book>
        <Title>B2</Title>
        <Year>2011</Year>
    </Book>
    <Book>
        <Title>B3</Title>
        <Year>2012</Year>
    </Book>
</Books>

我将通过我的 xQuery 代码获得两个 AAA。我想知道我是否可以获得不同的结果,这意味着只有一个 AAA。我已经尝试过 distinct-value() 但可能不知道如何使用它。感谢您的回复!

----我为 xQuery 1.0 使用 XML 格式更新的解决方案----

<root>
{
    for $x in distinct-values
    (
        for $r in doc("review.xml")//Reviews//Review,
            $b in doc("book.xml")//Books//Book
        where $b/Title = $r/BookTitle
            and $b/Year > 2010
        return {$r/Reviewer}
    )
    return <reviewer>{$x}</reviewer>
}
</root>
4

2 回答 2

1

以下查询将为您提供所有不同的审阅者名称(请注意,这些值是原子化的,这意味着元素节点已被删除):

distinct-values(
  for $r in doc("review.xml")//Reviews//Review,
      $b in doc("book.xml")//Books//Book
  where $b/Title = $r/BookTitle
    and $b/Year > 2010
  return $r/Reviewer
)
于 2012-04-09T19:59:08.753 回答
1

要保留节点,您可以使用“group by”子句并选择组序列的第一项:

for $r in doc("review.xml")//Review,
    $b in doc("book.xml")//Book
let $n := $r/Reviewer
where $b/Title = $r/BookTitle
  and $b/Year > 2010
group by $n
return $r[1]/Reviewer
于 2012-04-09T20:49:32.037 回答