Search code examples
xmlxquerymarklogicmarklogic-9

Search only those XMLs when a particular element occurs multiple times in Marklogic


I am trying to search for document XMLs in Marklogic which have the elements <document> more than once. Following is the structure of one such document XML i want to retrieve:

<root>
    <documents>
        <document>
            <id>1</id>
            <name>roy</name>
            <otherData></otherData>
        </document>
        <document>
            <id>2</id>
            <name>roy</name>
            <otherData></otherData>
        </document>
        ....
        ...
        ..
        .
        <document>
            <id>3</id>
            <name>roy</name>
            <otherData></otherData>
        </document>

    </documents>
</root>

I do not want to retrieve the XMLs which are of the following structure:

<root>
    <documents>
        <document>
            <id>3</id>
            <name>roy</name>
            <otherData></otherData>
        </document>
    </documents>
</root>

I can search for existence or minimum one using element-query with xs:QName("document"), but not sure how to go about searching with more than one.

Any help would be much appreciated.


Solution

  • There is no real simple way of doing this that scales well in MarkLogic. The easiest way out is by enriching the documents, adding a count attribute to the <documents> element, and keeping it up to date each time you touch the document. You can then do a straight-forward range index on the count attribute, and directly get what you are after.

    HTH!