【问题标题】:How to get the items count in xml using marklogic如何使用marklogic获取xml中的项目数
【发布时间】:2018-09-14 05:12:57
【问题描述】:

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


【解决方案1】:

我建议将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)

【讨论】:

    【解决方案2】:

    试试这个-

    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)
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 2013-09-24
      • 2023-02-26
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多