i have xml containing books @ library. want list books arent checked out. m approach books , if book id matches checked out book id not list it, otherwise list it.
in java or language double loop , loop on elements there similar 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>
in java or language double loop , loop on elements there similar xquery?
xquery has "for" loop/clause.
here couple of examples.
the first example if <book>
, <borrowers>
in different files:
books.xml
(notice second book has asin
matches asin
in borrowers.xml file.)
<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 $book in doc("books.xml")/books/book not($borrowed[@asin = $book/@asin]) return $book } </availablebooks>
results
<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>
here's example <book>
, <borrower>
data combined in single file:
(note: results same above.)
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> { $library in doc("combined.xml")/library $book in $library/books/book let $borrowed := $library/borrowers/borrower/borrowed/book not($borrowed[@asin = $book/@asin]) return $book } </availablebooks>
Comments
Post a Comment