Wednesday, December 26, 2007

Specifying a Document in XQLPlus

In a previous blog entry, I briefly summarized some information regarding XQLPlus. In this blog entry, I focus on how to specify a file in the local file system to XQLPlus via the XQuery function fn:doc.

The example XML for this example is shown below and is saved as a file called C:\xquery\sample1.xml. Here are the contents of C:\xquery\sample1.xml:

<?xml version="1.0"?>
<Person id="1" employee="true">
<Person id="2" employee="false">
<Employer>Fictitious and Sons</Employer>
<Person id="3" employee="true">

One can use the XQuery function fn:doc to access this file in an XQuery expression within XQLPlus, but the path must be carefully specified when it is passed to the fn:doc function to work properly. It is important to specify the protocol for accessing the XML document. In this case, the file is on my local hard drive (C:\), so I will use the file protocol. The other interesting twist is that the path separator following the C: in the document string needs to be a forward slash ("/" in Unix/Linux style) rather than the backward slash style (\) normally associated with Windows directory structures.

The following screen snapshot (click on image to see larger version) demonstrates a very basic XQuery expression designed to print out the same XML body as the source, but with the element NewPeople replacing the element name People.

The first attempt, passing a document path and name without the file protocol and using the normal Windows back slashes, results in an error message: "FODC0005: invalid argument to fn:doc." In the second attempt, the slashes are switched to forward slashes and there is no error message, but only an empty <NewPeople> element tag is created. In the third attempt, the forward slashes are retained and file:/// is prepended to the document's location. This attempt works as we hoped and the XQuery expression returns the new XML. Although not shown in the snapshot above, the same error "FODC0005: invalid argument to fn:doc" is encountered when Windows-style back slashes are used with the file protocol.

It is helpful to be able to run XQuery expressions in XQLPlus against XML files stored on the file system. This is often more practical than loading the XML source to be queried into XQLPlus. As shown above this is easily accomplished as long as the String which is passed to the fn:doc function is built correctly with forward slashes and the file protocol.

No comments: