xquery for 循环问题 [英] xquery for loop questions

查看:24
本文介绍了xquery for 循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含图书馆书籍的 xml.我想列出所有没有签出的书.所以 m 方法是获取所有书籍,如果书籍 id 与已检出的书籍 id 匹配,则不列出它,否则列出它.

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.

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

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>

推荐答案

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

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

XQuery 也有一个for"循环/子句.

XQuery also has a "for" loop/clause.

这里有几个例子.

第一个例子是如果 在不同的文件中:

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

books.xml

(请注意,第二本书的 asin 与 Boringers.xml 文件中的 asin 匹配.)

(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>

结果

<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:

(注:结果同上.)

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>

这篇关于xquery for 循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆