如何使用Marklogic获取xml中的项目计数

时间:2018-09-14 05:12:58

标签: xquery marklogic

我是MarkLogic的新手。.我需要从以下XML中获取书籍的总数。谁能建议我。

<bk:bookstore xmlns:bk="http://www.bookstore.org">
<bk:book category='Computer'>
    <bk:author>Gambardella, Matthew</bk:author>
    <bk:title>XML Developer's Guide</bk:title>
    <bk:price>44.95</bk:price>
    <bk:publish_year>1995</bk:publish_year>
    <bk:description>An in-depth look at creating applications with XML. 
</bk:description>
</bk:book>
<bk:book category='Fantasy'>
    <bk:author>Ralls, Kim</bk:author>
    <bk:title>Midnight Rain</bk:title>
    <bk:price>5.95</bk:price>
    <bk:publish_year>2000</bk:publish_year>
    <bk:description>A former architect battles corporate zombies, an evil 
 sorceress, and her     own childhood to become queen of the world. 
</bk:description>
</bk:book>
<bk:book category='Comic'>
    <bk:author>Robert M. Overstreet</bk:author>
    <bk:title>The Overstreet Indian Arrowheads Identification </bk:title>
    <bk:price>2000</bk:price>
    <bk:publish_year>1991</bk:publish_year>
    <bk:description>A leading expert and dedicated collector, Robert M. 
    Overstreet has been writing The Official Overstreet Identification and 
 Price 
  Guide to Indian Arrowheads for more than 21 years</bk:description>
  </bk:book>
 <bk:book category='Comic'>
    <bk:author>Randall Fuller</bk:author>
    <bk:title>The Book That Changed America</bk:title>
    <bk:price>1000</bk:price>
    <bk:publish_year>2017</bk:publish_year>
    <bk:description>The New York Times Book Review Throughout its history 
   America has been torn in two by debates over ideals and beliefs. 
  </bk:description>
  </bk:book>
  </bk:bookstore>

有人是我的新手吗?

2 个答案:

答案 0 :(得分:1)

Id建议结合使用cts:count-aggregatects:element-reference。这要求您在book上有一个element range index

cts:count-aggregate(cts:element-reference(fn:QName("http://www.bookstore.org", "book")))

如果性能不是很关键,并且文档数也不太大,也可以使用fn:count来计数。

declare namespace bk="http://www.bookstore.org";
fn:count(//bk:book)

答案 1 :(得分:0)

试试这个-

declare namespace bk="http://www.bookstore.org";

let $book_xml := 
<bk:bookstore xmlns:bk="http://www.bookstore.org">
  </bk:book>
   ........
   ........
  </bk:book>
</bk:bookstore>

return fn:count($book_xml//bk:book)

希望有帮助!