在Java或其他語言,我會做一個雙重for循環並遍歷所有的元素是有使用XQuery類似的東西?
XQuery還有一個「for」循環/子句。
這裏有幾個例子。
第一個例子是,如果<book>
和<borrowers>
在不同的文件:
的books.xml
(請注意,第二本書具有一個asin
在borrowers.xml文件相匹配的asin
。)
<books>
<book asin="0201100886" created="128135928" lastLookupTime="128135928">
<uuid>BA57A934-6CDC-11D9-830B-000393D3DE16</uuid>
<title>Compilers</title>
<authors>
<author>Alfred V. Aho</author>
<author>Ravi Sethi</author>
<author>Jeffrey D. Ullman</author>
</authors>
<publisher>Addison Wesley</publisher>
<published>1986-01-01</published>
<price>102.00</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="DEVNULL" created="128135928" lastLookupTime="128135928">
<uuid>98374982739847298347928374</uuid>
<title>Test Book</title>
<authors>
<author>DevNull</author>
</authors>
<publisher>Stackoverflow</publisher>
<published>2011-04-29</published>
<price>FREE</price>
<purchaseDate>2011-04-29</purchaseDate>
</book>
</books>
borrowers.xml
個
<borrowers>
<borrower id="1">
<name> John Doe </name>
<phone> 555-1212 </phone>
<borrowed>
<book asin="0138613370"/>
<book asin="0122513363"/>
<book asin="DEVNULL"/>
</borrowed>
</borrower>
</borrowers>
的XQuery
<availableBooks>
{
let $borrowed := doc("borrowers.xml")/borrowers/borrower/borrowed/book
for $book in doc("books.xml")/books/book
where not($borrowed[@asin = $book/@asin])
return $book
}
</availableBooks>
結果
<availableBooks>
<book asin="0201100886" created="128135928" lastLookupTime="128135928">
<uuid>BA57A934-6CDC-11D9-830B-000393D3DE16</uuid>
<title>Compilers</title>
<authors>
<author>Alfred V. Aho</author>
<author>Ravi Sethi</author>
<author>Jeffrey D. Ullman</author>
</authors>
<publisher>Addison Wesley</publisher>
<published>1986-01-01</published>
<price>102.00</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
</availableBooks>
下面是與<book>
和<borrower>
數據組合在單個文件中的另一個示例:
(注意:結果是與上述相同。)
combined.xml
<library>
<books>
<book asin="0201100886" created="128135928" lastLookupTime="128135928">
<uuid>BA57A934-6CDC-11D9-830B-000393D3DE16</uuid>
<title>Compilers</title>
<authors>
<author>Alfred V. Aho</author>
<author>Ravi Sethi</author>
<author>Jeffrey D. Ullman</author>
</authors>
<publisher>Addison Wesley</publisher>
<published>1986-01-01</published>
<price>102.00</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="DEVNULL" created="128135928" lastLookupTime="128135928">
<uuid>98374982739847298347928374</uuid>
<title>Test Book</title>
<authors>
<author>DevNull</author>
</authors>
<publisher>Stackoverflow</publisher>
<published>2011-04-29</published>
<price>FREE</price>
<purchaseDate>2011-04-29</purchaseDate>
</book>
</books>
<borrowers>
<borrower id="1">
<name> John Doe </name>
<phone> 555-1212 </phone>
<borrowed>
<book asin="0138613370"/>
<book asin="0122513363"/>
<book asin="DEVNULL"/>
</borrowed>
</borrower>
</borrowers>
</library>
的XQuery
<availableBooks>
{
for $library in doc("combined.xml")/library
for $book in $library/books/book
let $borrowed := $library/borrowers/borrower/borrowed/book
where not($borrowed[@asin = $book/@asin])
return $book
}
</availableBooks>
你應該發佈一個你正在嘗試做的例子。這聽起來像一個簡單的XPath將返回你所需要的,但很難說沒有一個例子。 – 2011-04-30 05:05:40
<書ASIN = 「0201100886」 \t創建= 「128135928」 \t lastLookupTime = 「128135928」> BA57A934-6CDC-11D9-830B-000393D3DE16