Count nodes
File: Data.xml <?xml version="1.0"?> <Books> <Book>book 1</Book> <Book>book 2</Book> <Book>book 3</Book> </Books> File: Query.xquery <library count="{count(doc("Data.xml")/Books/Book)}">{ for $b in doc("Data.xml")/Books/Book return <book>{$b/text()}</book> }</library> Output: <?xml version="1.0" encoding="UTF-8"?> <library count="3"> <book>book 1</book> <book>book 2</book> <book>book 3</book> </library>