How to get the filename being processed in the xquery script

714 views
Skip to first unread message

alfredk

unread,
Aug 8, 2012, 9:40:29 AM8/8/12
to zorba...@googlegroups.com
Hello,

I'm analyzing an XML file with zorba command line like this:
"C:\Program Files (x86)\Zorba XQuery Processor 2.5.0\bin\zorba.exe" --omit-xml-declaration -e input-context="<path to a file>" -q "<xquery file>" -f --context-item "<xml file to process>"

In the <xml file to process>, I have some relative references to other files: <include name="../../res/other_xml.xml"/>

I want to open this file in the XQuery script, but how I can open ../../res/other_xml.xml? Is there a way to access to the context-item value from the XQuery script, I tried: base-uri, static-base-uri, document-uri, resolve-uri, but none of them give me the right result. 

Any idea of what I should do?

Thanks a lot

William Candillon

unread,
Aug 8, 2012, 10:50:02 AM8/8/12
to zorba...@googlegroups.com
Hello,

You can setup manually an external variable that will contains the
base path for resolving your include references.
Another solution solution is to use the file module at
http://www.zorba-xquery.com/html/modules/expath/file in order to
resolve includes against the current working directory.

Does this help?

Kind regards,

William
> --
> You received this message because you are subscribed to the Google Groups
> "Zorba Users" group.
> To post to this group, send email to zorba...@googlegroups.com.
> To unsubscribe from this group, send email to
> zorba-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/zorba-users/-/mkZmt3fMo4EJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

alfredk

unread,
Aug 8, 2012, 10:58:39 AM8/8/12
to zorba...@googlegroups.com
Hi William,

Thanks for your replay. I had a look at the module expath/file, but files are not relative to the working folder unfortunately. So, I guess that I have to use external variables. I thought there was a shortcut to get the file path used in the input-context or a predefined external variable for this since it is already passed in the command line.

Whatever, I will try with  external variables.

Thanks

Fred

William Candillon

unread,
Aug 8, 2012, 11:08:59 AM8/8/12
to zorba...@googlegroups.com
Hello,

If you send a tiny query and data example, maybe we can look for a
solution that doesn't use an external variable.

Kind regards,

William
> https://groups.google.com/d/msg/zorba-users/-/urjg9X55DTQJ.

alfredk

unread,
Aug 8, 2012, 11:48:26 AM8/8/12
to zorba...@googlegroups.com
Hello,

Actually, I'm parsing an Android manifest file and I have something like this:

The Manifest file is located in a file <DIR>\AndroidManifest.xml with the following content:

<manifest ...>
   <application android:label="@string/app_name">
   ....
</manifest>

I want to get the value of  the label "@string/app_name". To do that, I have to look into a file located here:

<DIR>\res\values\strings.xml where I have:
<resources>
  <string name="app_name">MyApp</strings>
  ...
</resources>

So when parsing the manifest file, I have to open the file which is located relatively to the folder where the manifest file is: <DIR>

Thanks

Fred

Chris Hillery

unread,
Aug 8, 2012, 4:39:09 PM8/8/12
to zorba...@googlegroups.com
Certainly the way we have solved similar problems in the past was to pass the root directory as an external variable, and open all documents in the query by forming paths based on that variable.

You *can* get the URI to a document file by using fn:document-uri(), but only if the document was originally opened using the fn:doc() function. So for example,

declare variable $x as xs:string external;
let $d := fn:doc($x)
return fn:document-uri($d)

If you invoke this with "zorba -f -q /path/to/query -e x:=/path/fo/file.xml", then the result will be "file:///path/to/file.xml".  You can then generate new relative URIs using that as a base URI. However, I think it's probably easier to just pass the root directory as a string external variable in the first place.

If you're curious, the reason that it doesn't work the way you wish it did is because Zorba creates internal entries in its store for all documents that are opened. These store entries have internally-generated URIs because they are copies of the original and can be modified through updates. My understanding is that we have to do some special-case magic in the case of fn:doc() specifically to make fn:document-uri() work according to the spec.

However, I could possibly see an argument for Zorba's -e and --context-item command-line arguments behaving like fn:doc() in this way; should I file a bug regarding that? It's quite possible there are good reasons NOT to do this, but I'm not the best person to talk about what they might be.

Ceej
aka Chris Hillery

To view this discussion on the web visit https://groups.google.com/d/msg/zorba-users/-/gK0M60VAXYQJ.

alfredk

unread,
Aug 9, 2012, 8:21:33 AM8/9/12
to zorba...@googlegroups.com
Hello Chris,

Thanks you very much for your answer. Actually, I used the variable to pass the full file name to the xquery script and then I can do what I want to achieve. Having a working directory is a good idea too, but it requires that the structure of the project to be the same for all the cases you have to manage but when the only rule is that the path in the XML is expressed relatively to this XML, then the solution that will work in all cases is to get the XML file path.

I think that it would be good to store somewhere in memory the physical uri of documents that has been passed to the xquery script (either with --context-item file or -e variable=file) , so that the fn:document-uri() would work in all cases. If not, the behavior of: base-uri, document-uri and resolve-uri are really disappointing. So, I think that would be a good idea to fill a bug.

Thank you all for your answers.

Fred
Reply all
Reply to author
Forward
0 new messages