Querying Multiple Files in a Directory


DataDirect XQuery supports the use of fn:collection() to query multiple XML and non-XML files in a specified directory.

XML Files

In the following example, suppose you have a number of XML files stored in the directory books. Each of the files contains information about one book, and you want to create a single XML document that contains a list of all your books.

<books>{ 
for $book in collection("file:///c:/books?select=*.xml") 
return 
  <myBook>{$book/book/title}</myBook> 
</books> 

The result would look something like this:

<books> 
  <myBook> 
    <title>Emma</title> 
  </myBook> 
  <myBook> 
    <title>Pride and Prejudice</title> 
  </myBook> 
  . . . 
</books> 

The function’s declaration for this feature is:

collection("directory_url(?option(;option)*)?") 

where:

directory_url is a URL referencing a directory. The URL must use the file:// scheme.

option is {(select="REGEX") | recurse={yes | no} | (sort=[a,t,r]+) | (xquery-regex=(yes|no))}

where:

select contains a regular expression (REGEX), which determines which files in the directory are selected. If select is not specified, any file is assumed.

sort determines how the retrieved files are sorted, as follows:

Non-XML Files

DataDirect XQuery also supports the use of fn:collection() to query non-XML files, such as XML Converters. For example:

fn:collection("converter:///EDI?file:///C:/myfolder") 

returns a sequence in which each item is an EDI file contained in the directory C:/myfolder.

DataDirect XQuery also supports additional arguments in fn:collection() to tune navigation of the specified directory:

fn:collection("converter:name:[property_name=value: | property_name=value: | 
...]?directory_url(?option(;option)*)?") 

where:

name is the name of the XML Converter. See the XML converter documentation for more information.

property_name=value are property values specific to the converter name. See the XML converter documentation for more information.

directory_url and option are the same as previously described for XML files in a directory.

The following are non-XML file examples:

fn:collection("converter:///EDI?file:///C:/myfolder")/X12 

In this example, X12 elements from all files in the directory C:\myfolder are retrieved.

fn:collection("converter:///EDI?file:///C:/myfolder?recurse=yes")/X12 

In this example, X12 elements from all files the directory C:\myfolder are retrieved, including the ones in sub-folders.

fn:collection("converter:///EDI?file:///C:/myfolder?select=*.x12;recurse=yes;
sort=a")/X12 

In this example, X12 elements from all files with extension .x12 in directory C:\myfolder are retrieved, including the ones in sub-folders, and they are sorted in ascending order.

See also "Collection URI Resolvers".