marklogic Working with fn:count, xdmp:estimate and cts:frequency Using fn:count() to get the number of matching documents


Example

The XML document, I will be using throughout the examples is -

<a>
    <b>test-value</b>
    <d>fragment-d</d>
    <c-root>
        <d>fragment-d</d>
        <e>fragment-e</e>
    </c-root>
</a>

The following queries returns the number of documents with value fragment-d for element d -

  • Using a cts:search

    fn:count(cts:search(fn:doc(), cts:element-value-query(xs:QName("d"), "fragment-d")))

  • Using XPath

    fn:count(fn:doc()[//d="fragment-d"]))