How to get children, but not one of their children?

Using MarkLogic, but this is probably a general XQuery question.

I have:

<library>
  <books>
    <book title="whatever">
      <publisher>Random</publisher>
      ....more children
    </book>
    <book title="another">
      <publisher>Simon</publisher>
      ....more children
    </book>
    ...
  </books>
</library>

I want to return all the [book] elements with their title attributes, but I DO NOT want any of the children of the book, for example. [Publisher]. How to do it in an orthodox and executive manner?

Correlated question: sometimes I may want to get some of the children, but not others, for example. get all the elements of the book and their children of the publisher, but not one of the other children.

Thank.

+5
source share
4 answers

XQuery ( XPath, XQuery) /library/books/book , - , . API , , , . API , , , . , , . XQuery:

for $b in /library/books/book
return <book title="{$b/@title}"/>
+4

, , ... , - . :

for $i in doc()//book
return <book>{
    for $a in $i/@*
    return $a
}</book>
+1

, , . , /@ , .

:

for $book in /library/books/book return <book>{$book/@*}</book>

:

for $book in /library/books/book return element book {$book/@*}
0

All Articles