Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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.

like image 832
VaclavDedik Avatar asked Jun 17 '11 13:06

VaclavDedik


People also ask

What is XQuery used for in XML file?

XQuery is a functional language that is used to retrieve information stored in XML format. XQuery can be used on XML documents, relational databases containing data in XML formats, or XML Databases. XQuery 3.0 is a W3C recommendation from April 8, 2014.

Which query language is used to retrieve data from XML document?

XQuery is a language for finding and extracting elements and attributes from XML documents.

How do I search for a string in XML?

You can search for XML documents using a standard keyword search against XML content (values between the XML tags). However, if the wt. index. enableXMLTagSearch property is set to true, then Windchill indexes the XML content, tags, and attributes.

What is XML querying?

XQL (XML Query Language) is a way to locate and filter the elements (data fields) and text in an Extensible Markup Language (XML) document. XML files are used to transmit collections of data between computers on the Web.


1 Answers

Since you're using eXist-db, you can use the util:document-name() function:

util:document-name($dvd)
like image 198
Joe Wicentowski Avatar answered Sep 20 '22 17:09

Joe Wicentowski