2013-04-22 98 views
0

我對xQuery非常陌生,我必須編寫查詢來執行以下操作;xQuery中的兩個元素排序

顯示按年份排序的書籍和文章。

我試過了以下;

for $b in doc("myDB.xml")/myDB 
order by $b/book/year, $b/article/year 
return $b/book, $b/article 

for $b in doc("myDB.xml")/myDB 
order by $b//year 
return $b/book, $b/article 

,它不是任何排序。

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> 

輸出是整個xml本身,然後是原始順序中的書籍和文章元素。

我真的需要FOR嗎?任何人都可以幫忙嗎?

回答

1

檢查這一項:

let $doc := doc("Your.xml)/myDB 
for $eachBook in $doc/child::* 
order by $eachBook/year 
return $eachBook 

OUTPUT:

<?xml version="1.0" encoding="UTF-8"?> 
<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>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> 
<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> 
<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>Professional XML</title> 
    <author> 
     <last_name>Evjen</last_name> 
     <first_name>Bill</first_name> 
    </author> 
    <year>2007</year> 
    <publisher>Wrox</publisher> 
    </book> 
<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> 
<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> 
<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> 
+0

它沒有顯示出結果。我正在使用xBase執行。 – nommyravian 2013-04-22 13:21:10

+0

您是否更改過doc(「Your.xml」),您需要放置XML文件的URI,或者您可以將myDB節點傳遞給$ doc – 2013-04-22 13:53:44

+0

是的,我將其更改爲doc(「myDB.xml」)如果您使用的是doc(「Your.xml」),您的XML和XQUERY必須位於我的xml文檔中,但不起作用。$ doc:=/myDB給出錯誤; [XPST0003]不完整的位置步驟 – nommyravian 2013-04-22 14:03:12