2013-04-22 32 views
1

我對xQuery非常陌生,我必須編寫查詢來執行以下操作;xquery中的if或where子句

查找誰撰寫了至少一本書,並在同一年

我寫了下面使用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的專家。有人可以幫忙嗎?

回答

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 
+0

我不認爲這會產生正確的結果。假設有一本由A和B撰寫的書以及僅在A年創作的一篇文章。您的查詢將輸出A和B,這是不正確的。 – dirkk 2013-04-22 16:00:47

+0

太棒了,它工作。我明白了。謝謝。 – nommyravian 2013-04-22 16:09:12

+0

@dirkk這是正確的,這不會返回不同的XML。它可以通過使用密鑰來工作,但是我不認爲這是OP的問題的基本要素,並且會使查詢複雜化。還要注意你的例子返回一個字符串,而不是XML。 – wst 2013-04-22 16:41:45

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 
+0

它的工作,但我不得不接受一個答案,所以我接受了誰發佈第一之一。也爲你的答案+1。謝謝 – nommyravian 2013-04-22 16:09:50