Good morning,

Is it possible to use custom functions within an XPath expression without 
impeding ML's ability to use its universal index?

We defined custom functions in order to abstract a content model that may 
change between releases.  But, this approach doesn't scale.  Xdmp:plan() 
reveals XPath expressions using the functions require MarkLogic to filter out 
false positives.  Not true if we replace the calls to the custom functions with 
the markup we're trying to abstract: xdmp:plan()'s estimate then equals the 
number of actual results.

Here's an example of an XPath expression that gets everything it needs from the 
universal index:

/book[./mv:metadata/mv-lmd:layered/mv-lmd:color = 'red']

Here's the same using the custom function:

/book[rmd:get-lmd-value(., 'color') = 'red']

Below is the associated code.  "Lmd" stands for layered metadata.  Layered 
metadata may repeat, which is why get-lmd-value() takes the first value from 
get-lmd-values().  And, in this case, $mo is a <book> element.

declare function get-lmd-values(
        $mo as element(),
        $name as xs:string) as xs:string* {
    $mo/mv:metadata/mv-lmd:layered/node()[fn:node-name(.) eq 
xs:QName(fn:concat("mv-lmd:", $name))]
};

declare function get-lmd-value(
        $mo as element(),
        $name as xs:string) as xs:string {
    let $values as xs:string* := get-lmd-values($mo, $name)
    return if ($values) then xs:string($values[1]) else ""
};

Thank you.

-Brent
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to