In a recent newsgroup posting, Jake Segers asked how to display the results of FOR XML queries generically when the schema is inlined using the XMLDATA option. The XMLDATA option inlines an XDR schema within the result.  He wanted null values in SQL Server, which are represented in XML as the lack of the node's presense, to be represented within an HTML table as an empty cell. Jake's solution used RAW mode for the XML query, so we could guarantee the structure (AUTO mode queries define the structure according to the joined tables in the order they appear in the SELECT list).  He was unable to alter the solution to use EXPLICIT mode queries, so we turned to an XSLT solution to accomodate.

The problem is two-fold. First, you need to use the schema as the basis of what to display, not the data that is present (the data may not be there). Simply setting up match patterns for the attributes in the row elements would not work, as the missing attributes are not accomodated.  Second, the actual namespace may change in subsequent FOR XML queries. This is problematic, since you cannot reliably look for nodes within a given namespace, you must treat the nodes as if the namespace is irrelevant.

Here is what I came up with.

<xsl:stylesheet version ="1.0" exclude-result-prefixes ="data dt" xmlns:xsl="" xmlns:data="urn:schemas-microsoft-com:xml-data"
<xsl:output method ="html" />
<xsl:template match ="/" >
<xsl:apply-templates select ="/*/data:Schema" />
<xsl:apply-templates select ="/*/*[name()='row']" />
<xsl:template match ="/*/*[name()='row']" >
Variable is used to lookup the actual column names in the
 	XDR Schema attributes section
<xsl:variable name ="curNode" select ="current()" />
<xsl:for-each select ="/*/data:Schema/data:ElementType/data:attribute" >
<xsl:variable name ="typeName" select ="@type" />
<xsl:value-of select ="$curNode/attribute::*[local-name() = $typeName]" />
<xsl:template match ="data:Schema" >
<xsl:apply-templates select ="data:ElementType/data:attribute" />
<xsl:template match ="data:attribute" >
<xsl:value-of select ="@type" />