XQuery is a very cool new and upcoming language. It's still not a W3C final recommendation, but Stylus Studio has a great implementation.
With Biztalk and working with XML, I have often found it is nice to scan all the files in a directory and extract some xml tags from each file. XQuery doesn't natively support this, but the following will handle it. Unfortunately, it requires a few lines of Java Code (doesn't seem to support .NET yet).
declare namespace ext = "FileUtils";
declare function ext:listFiles($path as xs:string) as xs:string* external;
declare variable $dirname as xs:string := "file:///c:/xmltest";
<Sample>
{
for $filename in fn:tokenize(ext:listFiles($dirname), ",")
[fn:matches(., ".xml")]
, $rootel in doc(fn:concat($dirname, "/", fn:encode-for-uri($filename)))
return
<filename name='{$filename}'>
<root>{$rootel//*[local-name()='AdminError']/*[local-name()='CorrelationId'][1]}</root>
</filename>
}
</Sample>
Create a file called: .java - you don't even have to compile it.
This is an external function that we call from the XQuery program.
import java.net.*;
import java.io.*;
public class FileUtils
{
public static String listFiles(String root) throws URISyntaxException
{
File f = new File(new URI(root));
String[] list = f.list();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < list.length ; i++)
{
if(sb. length()>0)
sb.append(",");
sb.append(list[i]);
}
return sb.toString();
}
}
NOTE 1: be sure and use the 3 slashes between file:///c:/xmltest.
NOTE 2: the function "fn:encode-for-uri" is used because we were dealing with Biztalk files that had curly braces around the GUID. The special characters in the filenam was causing the XQuery to "choke" without this function.
Here's an example that reads a single file. I needed a quick list of all the credit card expiration dates in a file (this selects only the non-blank ones, then even sorts them). I was trying to figure out if they were YYMM or MMYY.
<html>
{
let $file := doc("ISAFeed.xml")
for $item in $file//viewfmt
where $item/ccardex > ' '
order by $item/ccardex
return <ExpireDate>{$item/ccardex}</ExpireDate>
}
</html>
The above use the FLWR statement (pronounced "flower" and stands for: For/Let/Where/Return. The curly-braces surround substitution of variables.
Comments begin and end with happy faces (: for example :)