Search code examples
marklogicmarklogic-8

Get XPath of Child node only by passing partial node name


I Have following XML Stored in MarkLogic,

<testDoc>
    <test>
        <test1>test1</test1>
        <test2>test2</test2>
        <test3>test3</test3>
    </test>
</testDoc>

My Requirement is to get the xpath of child nodes only like, if i pass test(partial node name) to my xquery then i am expecting

/testDoc/test/test1
/testDoc/test/test2
/testDoc/test/test3

but my xquery returning,

/testDoc
/testDoc/test
/testDoc/test/test1
/testDoc/test/test2
/testDoc/test/test3

XQuery I am Executing on qconsole is,

 xquery version "1.0-ml";
 let $xml := document { fn:doc("/test/testDoc")}
 let $elem-name := "test"
 let $elems := $xml//*[local-name()[contains(lower-case(.), lower-case($elem-name))]]
 return $elems ! xdmp:path(.)

Please guide me to achieve my requirement.


Solution

  • I'm not sure if you are trying to match at a certain depth or just the leaf nodes. If you only want to get the leaf nodes, you can add a predicate to check that there are no child elements

    [fn:not(./*)]
    

    You can also rewrite your predicates and use fn:matches() to do the match

    let $elems := $xml//*[fn:not(./*)][fn:matches(fn:local-name(.), $elem-name, "i")]