如何使用marklogic在xml中获取项目数

问题描述 投票:0回答:2

我是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>

任何人都可以找到这个问题的解决方案,因为我是新手。

xquery marklogic
2个回答
1
投票

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

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)

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)

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.