xquery for 循环问题

发布于 2024-11-03 21:20:46 字数 1035 浏览 1 评论 0原文

我有一个包含图书馆书籍的 xml。我想列出所有未借出的书。所以我的方法是获取所有书籍,如果书籍 ID 与已签出的书籍 ID 匹配,则不列出它,否则列出它。

在java或其他语言中,我会做一个双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>

I have a xml containing books at a library. I want to list all the books that arent checked out. So m approach is to get all the books and if the book id matches a checked out book id do not list it, otherwise list it.

In java or another language I would do a double for loop and loop over the elements is there something similar with 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-11-10 21:20:46

在java或其他语言中,我会做一个双重for循环并循环遍历元素,是否有与xQuery类似的东西?

XQuery 也有一个“for”循环/子句。

这里有几个例子。

第一个示例是,如果 位于不同的文件中:

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>

这是另一个将 数据合并在一个文件中的示例:(

注意:结果与上面相同.)

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>

In java or another language I would do a double for loop and loop over the elements is there something similar with xQuery?

XQuery also has a "for" loop/clause.

Here are a couple of examples.

The first example is if the <book> and <borrowers> are in different files:

books.xml

(Notice that the second book has an asin that matches an asin in the 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
for $book in doc("books.xml")/books/book
  where 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 another example with the <book> and <borrower> data combined in a single file:

(Note: The results are the same as 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>
{
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>
探春 2024-11-10 21:20:46

假设 元素是 元素的子元素(以使 XML 格式良好) ,这只是

/library/book[not(@asin = /library/borrowers/borrower/book/@asin)]

Assuming the <book> and <borrower> elements are children of a <library> element (to make the XML well formed), it's just

/library/book[not(@asin = /library/borrowers/borrower/book/@asin)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文