繁体   English   中英

xquery for 循环问题

[英]xquery for loop questions

我在图书馆有一个包含书籍的 xml。 我想列出所有未检出的书籍。 所以 m 方法是获取所有书籍,如果书籍 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>

在 java 或其他语言中,我会做一个双重 for 循环和循环元素,xQuery 有类似的东西吗?

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

这里有几个例子。

第一个例子是如果<book><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>

借款人.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>数据组合在一个文件中的示例:

(注:结果同上。)

结合.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>

假设<book><borrower>元素是<library>元素的子元素(以使 XML 格式良好),它只是

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM