我有一个包含图书馆书籍的xml。我想列出所有未签出的书籍。因此,方法是获取所有书籍,如果书籍ID与签出的书籍ID匹配,则不列出它,否则列出它。
在java或其他语言中,我会做一个双循环和循环遍历元素是否与xQuery类似?
<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>
<borrowers>
<borrower id="1">
<name> John Doe </name>
<phone> 555-1212 </phone>
<borrowed>
<book asin="0138613370"/>
<book asin="0122513363"/>
</borrowed>
</borrower>
</borrowers>
答案 0 :(得分:2)
在java或其他语言中,我会做一个双循环和循环遍历元素是否与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>
答案 1 :(得分:2)
假设<book>
和<borrower>
元素是<library>
元素的子元素(为了使XML格式良好),它只是
/library/book[not(@asin = /library/borrowers/borrower/book/@asin)]