简体   繁体   中英

How do I get output for a XQuery in MarkLogic in a one line output?

Will elaborate - when I execute the following command:

let $value := xdmp:forest-status(
                xdmp:forest-open-replica(
                  xdmp:database-forests(xdmp:database("Documents"))))
return $value

Above query returns a lot of information about the database "Documents" forest, like - forest-id, host-id, etc.

I only require that it should return only the "state" of my forest. How do I do that?

Use XPath to select what you want to return.

let $value := xdmp:forest-status(
                xdmp:forest-open-replica(
                  xdmp:database-forests(xdmp:database("Documents"))))
return $value/*:state/text()

Also, no need for a FLWOR you could make it a one-liner:

xdmp:forest-status(
  xdmp:forest-open-replica(
    xdmp:database-forests(xdmp:database("Documents"))))/*:state/text()

Or you may find that using the arrow operator makes things easier to read instead of nested function calls and tons of parenthesis wrapping them:

(xdmp:database("Documents")
  => xdmp:database-forests()
  => xdmp:forest-open-replica()
  => xdmp:forest-status()
)/*:state/text()

The XML elements in the response are in the http://marklogic.com/xdmp/status/forest namespace. So, you would either need to declare the namespace (ie declare namespace f = "http://marklogic.com/xdmp/status/forest"; ) and use the prefix in your XPath (ie f:state ), or just use the wildcard as I have done *:state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM