redjen
redjen

Reputation: 23

Getting location of XML document in XQuery

I'm using Oxygen XML to to work with an XML file that contains references to other XML files. These other XML files are stored in a subdirectory of the directory that contains the main XML file.

Downloads/
directory1/
  main.xml
  subdirectory1/
    other1.xml
    other2.xml

The locations of the other XML files are stored as relative links within the main file. My query looks like something this:

for $df in /clldata/DiscussionForums/DiscussionForum
    let $href := replace(data($df/@href),"\\","/")
    for $p in doc($href)
return $p

The problem I've run into is that Oxygen uses the path to the xquery file as the base for the relative path, obviously resulting in file not found errors. How can I get the path to the XML document within my query?

Right now I'm just using concat() to prepend the correct path but I want this to be easily reusable.

Upvotes: 2

Views: 2257

Answers (2)

MrWatson
MrWatson

Reputation: 516

I think the following XPath expression should select all linked documents:

/clldata/DiscussionForums/DiscussionForum/@href/doc(resolve-uri(., base-uri(.))

So you should be able to drill down to any xml element over all found documents like this:

/clldata/DiscussionForums/DiscussionForum/@href/doc(resolve-uri(., base-uri(.))
/path/to/the/element/you/want/to/find/in/all/documents/@name

If you prefer FLOWR style expressions, then something like this should do it:

for $doc in /clldata/DiscussionForums/DiscussionForum/@href/doc(resolve-uri(., base-uri(.))
where $doc/some/criteria='select'
return $doc/path/to/the/element/you/want/to/find/in/all/documents/@name

Upvotes: 0

Peter Hart
Peter Hart

Reputation: 4975

You need to resolve the URI relative to the URI of the main XML document. In your case, you probably want to do something like:

let $href = resolve-uri($d/@href, base-uri($d))

Here, the base-uri($d) function is getting the absolute URI of the source document containing the relevant DiscussionForum node, and the resolve-uri is resolving the new URI relative to this.

This works for me using SAXON on the jedit XQuery plugin. YMMV, I've never used Oxygen XML.

Upvotes: 2

Related Questions