This article describes how to transform the XML output of SQL query statements using FOR XML AUTO to HTML using XSLT.
Normally, we execute the query using ExecuteNonQuery statement and fill the output into a data table. We can access the data in the data table by giving column name and row number in the looping method. This makes us work on extra coding and takes a lot of time to get the correct data from executed query. So, I moved to XML because, in XML, we can fetch the values easily and quickly without writing too much code.
FOR XML Clause in SQL
Basically, we have four types of FOR XML clause in SQL.
- FOR XML RAW
- FOR XML AUTO
- FOR XML EXPLICIT
- FOR XML PATH
Each of the above types satisfies its own need. When you want full control over the produced XML, you use FOR XML EXPLICIT, but it is difficult to understand, read, or maintain. FOR XML AUTO produces the most readable SELECT statement. The RAW option is rarely used and therefore not discussed. The PATH option allows you to mix attributes and elements easier. Here, I gona use FOR XML AUTO.
Table Name - Contact_Details
Contact_ID | Contact_Number | Contact_Name | Contact_Location |
001 | 088888888 | Mohan | Chennnai |
002 | 099999999 | Abilash | Bangalore |
If we want the output as in XML format, we need to add the FOR XML AUTO statement.
- SELECT * FROM Contact_Details FOR XML AUTO
This query gives the output like,
- < Contact_Details Contact_ID=’001’ Contact_Number=’ 088888888’ Contact_Name=’Mohan’ Contact_Location=’Chennai’/>
- < Contact_Details Contact_ID=’002’ Contact_Number=’ 099999999’ Contact_Name=’Abilash’ Contact_Location=’Bangalore’/>
The XML which we got in output is not in user-friendly format. We need each field as an attribute so, that we can access the values using XPATH. To achieve that, we need to add the ELEMENTS parameter with the query.
- SELECT * FROM Contact_Details FOR XML AUTO, ELEMENTS
This query gives the output like,
- < Contact_Details>
- <Contact_ID>001</Contact_ID>
- <Contact_Number>088888888</Contact_Number>
- <Contact_Name>Mohan</Contact_Name>
- <Contact_Location>Chennai</Contact_Location>
- </ Contact_Details>
- < Contact_Details>
- <Contact_ID>002</Contact_ID>
- <Contact_Number>099999999</Contact_Number>
- <Contact_Name>Abilash</Contact_Name>
- <Contact_Location>Bangalore</Contact_Location>
- </ Contact_Details>
So, this is the exact XML format we need.
Now, we are going to create XML DOCUMENT and write this output XML into that XML document in coding. To read the query in XML format, we need to execute the query using ExecuteXmlReader() function.
- XmlReader XMLReader = new XmlReader();
- XmlDocument XmlDoc = new XmlDocument();
- SqlCommand Cmd = new SqlCommand(Query, Conn);
- XMLReader = cmd.ExecuteXmlReader();
- XPathDocument xPathDoc = new XPathDocument(XMLReader);
- XPathNavigator xPathNavi = xPathDoc.CreateNavigator();
- XmlDoc.LoadXml(XpathNavi.OuterXml);
Now, we got the full structured XML Document. Using this, we will transform the data into HTML format using XSLT.
Before transforming, we need to create one XSLT file to assign the values from XMLDocument to HTML format.
Contact_Details.xslt
- <?xml version=”1.0” encoding=”utf-8”?>
- <xsl:stylesheet version=”1.0” xmlns:xsl=”http:
- <xsl:output method=”xml” indent=”yes” omit-xml-declaration=”yes”>
- <xsl:template match=”/*”>
- <table>
- <tr>
- <td>ContactID</td>
- <td>ContactNumber</td>
- <td>ContName</td>
- <td>ContactLocation</td>
- </tr>
- <xsl:for-each select=”/ Contact_Details”>
- <tr>
- <td>
- <xsl:value-of select=”/Contact_details/Contact_ID” </td>
- <td>
- <xsl:value-of select=”/Contact_details/Contact_Number” </td>
- <td>
- <xsl:value-of select=”/Contact_details/Contact_Name” </td>
- <td>
- <xsl:value-of select=”/Contact_details/Contact_Location” </td>
- </tr>
- </xsl:for-each>
- </table>
- </xsl:template>
- </xsl:stylesheet>
Now, we have XML Document and XSLT file. In the next step, we are going to transform the XML Document into HTML using XSLT. To do this, we pass the XMLDocument and XSLT file to separate functions and get fully formed HTML strings in return.
- Public static string TransformXMLTOHTML(XmlDocument XMLDoc, String XSLTFilename) {
- StringWriter HTMLString = new StringWriter();
- XslCompiledTransform Xmltransform = new XslCompiledTransform();
- XmlTransform.Load(XSLTFilename);
- Xmltranform.Transform(XMLDoc, null, Result);
- Return HTMLString.ToString();
- }
I hope this article is very useful. Please visit my previous article to learn a little more about using XML Data.