我在图书馆有一个包含书籍的xml。我想把所有没借出的书都列出来。因此,m方法是获取所有图书,如果图书id与已签出的图书id匹配,则不列出它,否则列出它。
在java或其他语言中,我会做一个double for循环,循环遍历元素,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>发布于 2011-04-30 14:05:21
在java或其他语言中,我会做一个double
循环,循环遍历元素,xQuery也有类似的东西吗?
XQuery也有一个"for“循环/子句。
这里有几个例子。
第一个示例是如果<book>和<borrowers>位于不同的文件中:
books.xml
(请注意,第二本书有一个与borrowers.xml文件中的asin匹配的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>发布于 2011-05-01 05:39:03
假设<book>和<borrower>元素是<library>元素的子元素(为了使其格式正确),它只是
/library/book[not(@asin = /library/borrowers/borrower/book/@asin)]https://stackoverflow.com/questions/5839657
复制相似问题