0

The problem

I have a query that gets executed by eXist through the REST API, and it gets POSTed to the collection it needs to run on, e.g. /db/foo or /db/bar. In the query, I'd like to get the child collections of the current collection. That is: if posted on /db/foo, I'd like the query return what xmldb:get-child-collections('/db/foo') would return; if posted on /db/bar, I'd like the query return what xmldb:get-child-collections('/db/bar') would return. But of course, I don't want the path of the current collection to be hard-coded in the query.

What I tried

  • xmldb:get-child-collections('.'), no luck, . is not understood by eXist as "the current collection".
  • Looking for a function that returns the path to the current collection, like pwd would in a shell, but I couldn't find one.
TheRedOne
  • 165
  • 3
  • 12
avernet
  • 30,895
  • 44
  • 126
  • 163
  • I am not currently able to test these, but have you looked to see whether uu:escape-collection-path() and uu:unescape-collection-path() default to the current collection? theme:parent-collection()? Or less exist-specific, base-uri($n as node()) (with string manipulation to extract the name of the collection)? – C. M. Sperberg-McQueen Dec 13 '12 at 20:46
  • @C.M.Sperberg-McQueen Thank you for chiming in. I tried and `base-uri()` returns `err:XPDY0002: context sequence is empty and no argument specified [at line 1, column 1]`, and I don't see any of the other 3 functions listed in the eXist documentation: `uu:escape-collection-path()`, `uu:unescape-collection-path()`, `theme:parent-collection()`. Am I missing something? – avernet Dec 14 '12 at 00:45
  • You need to pass a node to base-uri -- if you select an arbitrary node in a collection (e.g. via `let $e := /*[1]`) and ask for its base URI, you should get better results. The other functions I found by clicking Browse on the [XQuery Function Documentation](http://exist-db.org/exist/xquery/functions.xql) page. Perhaps they are in eXist 2.0 but not the version of eXist you are running? – C. M. Sperberg-McQueen Dec 14 '12 at 01:09
  • Aha. Further wandering around the eXist site led to [a different function library search page for eXist 2.0](http://exist-db.org/exist/apps/fundocs/index.html) and the discover of a function called `util:collection-name`, which takes a node as argument and returns the name of the collection containing that node's document. – C. M. Sperberg-McQueen Dec 14 '12 at 01:29
  • @C.M.Sperberg-McQueen Good idea: `util:collection-name((/*)[1])` works, except if the current collection doesn't contain any document. In that case, (a) if it contain collections with documents, it returns the path to one of those collections, which is incorrect, and (b) if it doesn't contain any collection, it returns an empty sequence. This is a partial solution, which might work in some cases. – avernet Dec 14 '12 at 18:37
  • But suddenly, it strike me: I can just use `request:get-path-info()`, since the request is sent through the REST API. I'll post an answer to this question, and thank you for making me think more about this! – avernet Dec 14 '12 at 18:37

1 Answers1

0

With the REST API, you can use request:get-path-info() to get the path to the "current collection". For instance:

  • If you're doing a GET or POST to http://www.my-site.com/exist/rest/db/my-collection.
  • request:get-path-info() will return /db/my-collection, which is precisely the part of the path you're interested in.
avernet
  • 30,895
  • 44
  • 126
  • 163
  • You can see an example of this being used in actual code in Orbeon's eXist persistence layer. Look for `request:get-path-info()` in the XQuery embedded in https://github.com/orbeon/orbeon-forms/blob/master/src/resources/apps/fr/persistence/exist/form.xpl – avernet Dec 14 '12 at 19:00