XML gives web developers greater programming flexibility than HTML. This technology-driven development organization tries to integrate XML with its own products. Microsoft is the pioneer of such a move. XML is visible in almost all of Microsoft's products. For example: How does Microsoft integrate XML into its SQL Server product line? Let's take a look at the key FOR XML clause below.
Get information in the name of XML
The first thing about integration between SQL Server and XML is to create an XML file from SQL data. The structure of the XML file is not complex and can be easily generated with simple scripts and an ADO recordset. While this task is not a hassle, developers need to produce different scripts for the result sets they get from the server, or to write more complex generic scripts. The SELECT statement is thus equipped with a new FOR XML clause.
The syntax of the clause is as follows:
[For {XML {RAW | AUTO | EXPLICIT}
[, XMLDATA]
[, ELEMENTS]
[, BINARY BASE64]} ]
The XML schema for the FOR XML clause is represented by three parameter values: RAW, Auto, or explicit. The pattern determines the form and composition of the resulting XML. Let's take a closer look at each of these XML options with the following examples.
Raw sample
We execute the following SQL statement:
SET ROWCOUNT 3
SELECT Orders.OrderID, Orders.OrderDate, ProductID
FROM Orders, [Order Details]
WHERE Orders.OrderID = [Order Details].OrderID
ORDER BY Orders.OrderID
FOR XML RAW
The following results have been implemented:
Auto Sample
We execute the following SQL statement:
‘结果限制为3条记录。
SET ROWCOUNT 3
SELECT Orders.OrderID, Orders.OrderDate, ProductID
FROM Orders, [Order Details]
WHERE Orders.OrderID = [Order Details].OrderID
ORDER BY Orders.OrderID
FOR XML AUTO
produces the following results:
Explicit sample
The explicit mode gives the query programmer complete control over the resulting XML. But this control is expensive: you have to write each query so that SQL statements can contain XML information.
The syntax is complex and beyond the scope of this article. [, XMLDATA] [, ELEMENTS] [, BINARY BASE64] is the appropriate optional parameter.
Optional elements
The example allows us to learn more about the internal mechanism of the various settings, and we'll take a closer look at the optional element xmldata for the XML statement below.
If you set this option, the Xml-data schema will be included in the result set. The following is an SQL statement:
SET ROWCOUNT 3
SELECT Orders.OrderID, Orders.OrderDate, ProductID
FROM Orders, [Order Details]
WHERE Orders.OrderID = [Order Details].OrderID
ORDER BY Orders.OrderID
FOR XML AUTO, XMLDATA
The above SQL statement produces the following results:
"urn:schemas-microsoft-com:datatypes">
order="many">…
ELEMENTS
The elements option indicates that each data column is returned as a child element rather than as a property. If you use Auto mode, you can use this option only.
BINARY BASE64
Use this option to indicate that you want to represent binary data in the Base64 encoding format.
Note: This article is just a simple description of the FOR XML clause, and here's a reminder that this is a bit of an XML-SQL Server integration need to focus on, and other issues include IIS OpenXML functions and template files.