xQuery中的if或Where子句

时间:2013-04-22 15:36:13

标签: xml xquery

我是xQuery的新手,我要编写查询来执行以下操作;

  

查找所有在同一年至少撰写过一本书和一篇文章的作者

我使用IF-Then-Else子句写了以下内容;

for $a in doc("myDB.xml")/myDB
return if ($a/book//last_name/text() = $a/article//last_name/text() 
and $a/book/year = $a/article/year)
then $a//author
else()

Where条款。

for $a in doc("myDB.xml")/myDB
where $a/book//last_name/text() = $a/article//last_name/text() 
and $a/book/year = $a/article/year
return $a//author

这两种方法都归功于所有作者。

XML是;

<myDB>
    <book>
        <title>ebXML : Concepts and Application</title>
        <author>
            <last_name>Gibb</last_name>
            <first_name>Brian</first_name>
        </author>
        <year>2003</year>
        <publisher>Wiley</publisher>
    </book>

    <article>
        <title>test title</title>
        <author>
            <last_name>Gibb</last_name>
            <first_name>Brian</first_name>
        </author>
        <year>2003</year>
        <publisher>test Wiley</publisher>
    </article>

    <book>
        <title>XML: Visual QuickStart Guide</title>
        <author>
            <last_name>Goldberg</last_name>
            <first_name>Kevin</first_name>
        </author>
        <author>
            <last_name>Doe</last_name>
            <first_name>John</first_name>
        </author>

        <year>2008</year>
        <publisher>Peachpit Press</publisher>
    </book>
    <book>
        <title>Beginning XML</title>
        <author>
            <last_name>Hunter</last_name>
            <first_name>David</first_name>
        </author>
        <year>2007</year>
        <publisher>Wrox</publisher>
    </book>
    <book>
        <title>Learning XML </title>
        <author>
            <last_name>Ray</last_name>
            <first_name>Erik</first_name>
        </author>
        <author>
            <last_name>Smith</last_name>
            <first_name>David</first_name>
        </author>
        <year>2003</year>
        <publisher>O'Reilly</publisher>
    </book>
    <book>
        <title>Professional XML</title>
        <author>
            <last_name>Evjen</last_name>
            <first_name>Bill</first_name>
        </author>
        <year>2007</year>
        <publisher>Wrox</publisher>
    </book>
    <article>
        <title>FileNet Integrates a New XML Editor for Non-Techies</title>
        <author>
            <last_name>Roe</last_name>
            <first_name>David</first_name>
        </author>
        <journal>CMS Wire</journal>
        <year>2009</year>
    </article>
    <article>
        <title>The Groundbreaking Potential of Codeless Ajax</title>
        <author>
            <last_name>Dusoe</last_name>
            <first_name>Jim</first_name>
        </author>
        <journal>Tech News World</journal>
        <year>2009</year>
        <pages>4</pages>
    </article>
    <article>
        <title>Windows and .NET Watch: Recognizing DSL opportunities</title>
        <author>
            <last_name>O'Brien</last_name>
            <first_name>Larry</first_name>
        </author>
        <journal>Software Development Times</journal>
        <year>2009</year>
    </article>
    <article>
        <title>An embedded XML Database: Oracle Berkeley DB XML</title>
        <author>
            <last_name>Gibb</last_name>
            <first_name>Brian</first_name>
        </author>
        <journal>The Register</journal>
        <year>2003</year>
        <pages>8</pages>
    </article>
    <article>
        <title>Open Source PostgreSQL 8.3 Better Suited For Web Applications </title>
        <author>
            <last_name>Babcock</last_name>
            <first_name>Charles</first_name>
        </author>
        <journal>Information Week</journal>
        <year>2008</year>
        <pages>1</pages>
    </article>
</myDB>

我不是让那些在同一年写了至少一本书和一篇文章的作者,而是我得到了所有作者的名单。我知道查询中存在逻辑错误,因为我不是xQuery的专家。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

问题是XPath语句的行为与您在查询中的预期方式不同。它们只是选择XML序列 - 所以当你比较它们时,如果左边的任何值与右边的任何值匹配,它将返回true。因为每个比较都返回true,所以它总是返回$a//author,文档中的每个author

我想你想要:

for $b in doc("myDB.xml")/myDB/book
let $articles:= doc("myDB.xml")/myDB/article[year=$b/year]
where ($b/author = $articles/author)
return $b/author

答案 1 :(得分:0)

必须在迭代项上使用

for,但myDB只出现一次。

以下查询的作用是获取所有作者以及检查他们是否有与他们创作的图书年度相同的文章。

let $doc := doc("myDB.xml")/myDB
for $author in distinct-values($doc/*/author)
where $doc/article[author = $author][year = $doc/book[author = $author]/year]
return $author