13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The FOR <strong>XML</strong> EXPLICIT ModeThe query def<strong>in</strong>es the shape of the generated <strong>XML</strong> document. The ultimate goal of thequery is mak<strong>in</strong>g hierarchical data fit <strong>in</strong>to a tabular rowset. An EXPLICIT mode querycreates a virtual table <strong>in</strong> which all the <strong>in</strong><strong>for</strong>mation fetched from the tables is organized <strong>in</strong>such a way that it can then be easily rendered <strong>in</strong> <strong>XML</strong>. The def<strong>in</strong>ition of the schema isfree, and of course, programmers must ensure that the f<strong>in</strong>al output is well-<strong>for</strong>med <strong>XML</strong>.Any FOR <strong>XML</strong> EXPLICIT query requires two extra metacolumns, named Tag andParent. The values <strong>in</strong> these columns are used to generate the <strong>XML</strong> hierarchy. The Tagcolumn conta<strong>in</strong>s a unique numeric <strong>in</strong>dex <strong>for</strong> each <strong>XML</strong> root node that is expected tohave children <strong>in</strong> the <strong>XML</strong> schema. The Parent column conta<strong>in</strong>s a tag value that l<strong>in</strong>ks agiven node to a particular, and previously def<strong>in</strong>ed, subtree.To add columns, you must use a relatively complex syntax <strong>for</strong> column aliases. Eachselected column must have an alias def<strong>in</strong>ed accord<strong>in</strong>g to the follow<strong>in</strong>g syntax:SELECT column_name AS [ParentNode!ParentTag!TagName!Directive]TheParentNode item represents the name of the node element that is expected to bethe parent of the column data. The ParentTag is the tag number of the parent. TheTagName item <strong>in</strong>dicates the name of the <strong>XML</strong> element that conta<strong>in</strong>s the column data.F<strong>in</strong>ally, the Directive element can take various values, the most common ones be<strong>in</strong>g novalue or element. If no value is specified, the column data is rendered as an attributenamed TagName; otherwise, it will be an element attribute.It's <strong>in</strong>terest<strong>in</strong>g to note that an EXPLICIT mode query consists of one or more tables thatresult from SELECT statements potentially <strong>in</strong>volv<strong>in</strong>g multiple tables and jo<strong>in</strong>ed data.Let's see what's needed to obta<strong>in</strong> the follow<strong>in</strong>g <strong>XML</strong> representation of the rows <strong>in</strong> theNorthw<strong>in</strong>d database's Employees table:birthdatecityhiredatetitlenotesThe boldface l<strong>in</strong>es <strong>in</strong> this code represent the roots of the three subtrees of <strong>XML</strong> databe<strong>in</strong>g created. Each subtree corresponds to a different tag, and each must be filled byresort<strong>in</strong>g to a different SELECT statement.To beg<strong>in</strong> fill<strong>in</strong>g the subtrees, consider the follow<strong>in</strong>g query:SELECT 1 AS Tag,NULL AS Parent,employeeid AS [Employee!1!ID],289

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!