[xquery-talk] How to run xquery dynamically on file listing from directory

Frans Englich frans.englich at telia.com
Wed May 14 12:30:24 PDT 2008

On Thursday 10 April 2008 20:22:10 seleneplatt at comcast.net wrote:
> I'm a complete newbie at xquery and am trying to execute a query
> dynamically on all files in a given directory, rather than hardcoding a
> particular file into the 'doc()' call.  I've tried using 'collection()' but
> am unclear as to how to get it to work.
> I did see this thread on x-query.com:
> [xquery-talk] Query Through Multiple Files
> but the closest anyone came to the answer I was hoping to find was to
> hardcode a *list* of files to traverse.
> Is there anyway to do something like:
>     for $x in collection("xmlfiles")
> where 'xmlfiles' is a subdirectory containing the actual xml files I want
> to use?  I would need to know the exact syntax, as this is all new to me.

QtXmlPatterns[1] has an example which allows you to write code like this:

for $file in $fileTree//file[@suffix = "cpp"]
order by xs:integer($file/@size)
return <li>
           {string($file/@fileName)}, size: {string($file/@size)}

This query, which is copied from the "filetree" example[2], is operating on a 
custom node backend(subclass of QAbstractXmlNodeModel) which mirrors the file 
system as an XML tree. It allows the full aggregation/filtering power of 
XQuery to be used, and hence little reinvention.

However, this file system backend isn't builtin(I plan to have it, maybe for 
Qt 4.5), so one would have to copy the C++ code from the example into ones 
program, unfortunately.

But I think it's a clean approach for a common problem: "I want to select some 
files and then process them." At somepoint I wouldn't mind looking into 
vendor-neutrally specify an XML format & XQuery extensions for exactly this.





More information about the talk mailing list