简体   繁体   中英

XQuery: how to return nodes that occur in every element

<bookstore>

<book category="Programming">
  <title lang="en">Coding</title>
  <publisher>ErBooks</publisher>
  <field>web</field>
  <field>programming</field>
  <field>C++</field>
</book>

<book category="XML">
  <title lang="en">Hey XML</title>
  <publisher>BookyBooks</publisher>
  <field>web</field>
  <field>xml</field>
  <field>database</field>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <publisher>Penguin</publisher>
  <field>web</field>
  <field>design</field>
  <field>database</field>
</book>

I want to retrieve field(s) that every publisher has published books on (which in this sample case is "web").

Pseudo code: if all publishers = fieldX return fieldX

If every publisher has only one book, as in the example, you can directly check for every field, if all books have that field:

/book[1]/field[every $book in /book satisfies $book/field = .]

Otherwise you need to filter the books and consider each publisher separately:

let $bookstore := /bookstore  
let $publisher := distinct-values($bookstore/book/publisher)
let $fields := distinct-values($bookstore/book/field) 
return $fields[every $p in $publisher satisfies $bookstore/book[publisher = $p]/field = .]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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