VaclavDedik VaclavDedik - 5 months ago 19
Java Question

Getting filename of an XML file with xquery

I store my entities in the exist xml database and I use a file name (resource id) as an ID of an entity.

Example: String xquery = "for $movie in collection('/db/movie')//movie "
+ "return $movie";

After executing this query I retrieve org.xmldb.api.base.Resource instance whose content I use to create an entity. When I want to set an id of this entity, I do it like this:

dvd.setId(rs.getId());


The problem is that if I execute query like this:

String xquery = "for $dvd in collection('/db/dvd')//dvd "
+ "return <dvd>"
+ "{$dvd/title}"
+ "{$dvd/type}"
+ "{"
+ "<content>"
+ " {"
+ " for $movie in $dvd/content//movie"
+ " let $movieIn := doc(concat(\"/db/movie/\", $movie/@id))/movie"
+ " return "
+ " <movie id=\"{$movie/@id}\">"
+ " {$movieIn/name}"
+ " {$movieIn/director}"
+ " {$movieIn/year}"
+ " {$movieIn/country}"
+ " {$movieIn/actors}"
+ " {$movieIn/genres}"
+ " </movie>"
+ " }"
+ "</content>"
+ "}"
+ "</dvd>";


rs.getId() returns null. I also tried method getDocumentId() from this class http://xmldb-org.sourceforge.net/xapi/api/org/xmldb/api/modules/XMLResource.html .. but it returns null as well. Is there a way of making it return the id of the resource (which is the name of the file which the entity is stored in) ?

If it's not possible, is there a way (function or something) of getting the file name of the file which I'm working with (I mean, the database retrieves data from) with an xquery query ?

I tried replacing this line:

+ "return <dvd>"


with this:

+ "return <dvd id=\"{$dvd}\">"


(so that I could get the name of the file from the attribute) but it doesn't return the file name.

I hope it is comprehensible enough. Sorry for my english.

Thanks in advance.

Answer

You might be looking for fn:base-uri(). See here.