| Servergeek |
| Mickey Williams' weblog |
|
My C# BookMicrosoft Press PageOn Amazon Racing LinksSlalomSkateboarder.comNCDSA slalom page 3dm Ick Sticks Pocket Pistols .NETScott GuthrieRob Howard .NET Notes BradA LonghornScobleChris Sells ServicesDon BoxChristian Weyer All Things Distributed Ingo Rammer Tim Ewald BlawgsBag and BaggageSCOTUS Blog EconArg MaxCapital Spectator OtherWilliam Gibson |
Monday, February 16, 2004
John Gilmour versus the TreePosted 5:25 PMJohn Gilmour is one of the top skateboard slalom racers in the US, and placed 4th in the World Teleboard Championships at Wachusett on Sunday (a teleboard is like a narrow snowboard with telemark bindings.) After the race he was freeriding and had a personal encounter with a rogue tree, resulting in:
It looks like he'll be out for at least part of the skateboard racing season, which will be a great loss to everyone on the circuit this year.
XQuery Support in WhidbeyPosted 1:18 PMI've been experimenting with the XQuery support in the Whidbey pre-release. Once my head started getting around the programming model, I was able to build some fun demos. Here's one that queries two XML documents and builds a new third document. First, there's an XML document that contains author information:
<?xml version="1.0" encoding="utf-8" ?>
<authors xmlns="http://www.servergeek.com/2004/1/demos/xquery">
<author id="001">
<fname>Mickey</fname>
<lname>Williams</lname>
<proglangs>
<proglang>csharp</proglang>
<proglang>eiffel</proglang>
</proglangs>
</author>
...
</authors>
Next, there's an XML document that lists royalty information (given the current state of the book publishing business , it's obviously implied that these are annual royalties):
<?xml version="1.0" encoding="utf-8" ?>
<royalties xmlns="http://www.servergeek.com/2004/1/demos/xquery">
<royalty>
<id>001</id>
<authorid>002</authorid>
<amount>12.35</amount>
</royalty>
<royalty>
<id>002</id>
<authorid>003</authorid>
<amount>10.27</amount>
</royalty>
<royalty>
<id>003</id>
<authorid>001</authorid>
<amount>11.66</amount>
</royalty>
</royalties>
I often need to consume data (sometimes XML) from diverse sources and squirt the data through a new schema into a new data sink. For example, I might want to compose portions of the previous two XML documents into a new document that's passed to a third party. Pre-XQuery, if I wanted to join these two documents into a unified document with a new structure, I'd need to write at least a bit of code. With XQuery, I can join the two documents tgether and create a new document structure using syntax like this:
declare namespace au='http://www.servergeek.com/2004/1/demos/xquery'
<payments>
{
for $author in document('authors')/au:authors/au:author
return <authorPayment>
<authorID>{$author/@id}</authorID>
<name>{$author/au:lname/text()}, {$author/au:fname/text()}</name>
{
for $royalty in document('royalties')/au:royalties/au:royalty
where $author/@id = $royalty/au:authorid
return <royalty id="{$royalty/au:id/text()}">
<amount>{$royalty/au:amount/text()}</amount>
</royalty>
}
</authorPayment>
}
</payments>
Keep in mind that I've only got a short time with XQuery at this point, but it looks promising so far. Not a replacement for XPath, but probably very useful in scenarios where I'm using XPath to extract XML and create new documents. Here's the code that uses the query and two input documents to create a new result document:
static string UseDefaultValues()
{
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
Assembly asm = Assembly.GetExecutingAssembly();
using(StreamReader queryReader = new StreamReader(asm.GetManifestResourceStream("XqDemo.query.txt")))
using(XmlTextReader authorReader = new XmlTextReader(asm.GetManifestResourceStream("XqDemo.Authors.xml")))
using(XmlTextReader royaltyReader = new XmlTextReader(asm.GetManifestResourceStream("XqDemo.Royalties.xml"),
authorReader.NameTable))
{
XQueryProcessor qp = new XQueryProcessor();
XmlDataSourceResolver resolver = new XmlDataSourceResolver(authorReader.NameTable);
resolver.Add("authors", authorReader);
resolver.Add("royalties", royaltyReader);
qp.Compile(queryReader);
qp.Execute(resolver, writer);
}
return sb.ToString();
}
More coming, including a complete example...
|