IT tutorials
 
Database
 

SQL Server 2012 : XML and the Relational Database - Additional FOR XML Features (part 2)

7/23/2013 8:12:58 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Emitting a ROOT Element

Technically, an XML document must be contained inside of a single root element. You’ve seen many applied uses of FOR XML that generate all types of XML, but without a root element, the generated XML can only represent a portion of an XML document. The ROOT option allows you to add a main, or root, element to your FOR XML output so that the query results can be consumed as a complete XML document. You can combine ROOT with other FOR XML keywords. In Example 5, ROOT is used with FOR XML AUTO to wrap a single Orders root element around the results of the query.

Example 5. Using FOR XML with ROOT to generate a root element.

SELECT
  Customer.CustomerID,
  OrderDetail.SalesOrderID,
  OrderDetail.OrderDate
 FROM
  Sales.Customer AS Customer
   INNER JOIN Sales.SalesOrderHeader  AS OrderDetail
    ON OrderDetail.CustomerID = Customer.CustomerID
 WHERE
  Customer.CustomerID IN (11000, 11001)
 ORDER BY
  Customer.CustomerID
 FOR XML AUTO, ROOT('Orders')

The output looks like this:

<Orders>
  <Customer CustomerID="11000">
    <OrderDetail SalesOrderID="43793" OrderDate="2005-07-22T00:00:00" />
    <OrderDetail SalesOrderID="51522" OrderDate="2007-07-22T00:00:00" />
    <OrderDetail SalesOrderID="57418" OrderDate="2007-11-04T00:00:00" />
  </Customer>
  <Customer CustomerID="11001">
    <OrderDetail SalesOrderID="43767" OrderDate="2005-07-18T00:00:00" />
    <OrderDetail SalesOrderID="51493" OrderDate="2007-07-20T00:00:00" />
    <OrderDetail SalesOrderID="72773" OrderDate="2008-06-12T00:00:00" />
  </Customer>
</Orders>

The code output here is the same as any FOR XML AUTO output for this query, except that the XML ROOT we specified with the ROOT keyword now surrounds the data. In this example, we used ROOT (‘Orders’), so our output is surrounded with an <Orders> XML element.

4. Producing an Inline XSD Schema

As you’ve seen, schemas provide an enforceable structure for your XML data. When you export data using the FOR XML syntax, you might want to include an inline XML schema for the recipient so that the recipient can enforce the rules on their end as well. When you use the RAW and AUTO modes, you can produce an inline XSD schema as part of the output by using the XMLSCHEMA keyword, as shown in Example 6.

Example 6. Using FOR XML with XMLSCHEMA to generate an inline XSD schema with the query results.

SELECT
  Customer.CustomerID,
  OrderDetail.SalesOrderID,
  OrderDetail.OrderDate
 FROM
  Sales.Customer AS Customer
   INNER JOIN Sales.SalesOrderHeader  AS OrderDetail
    ON OrderDetail.CustomerID = Customer.CustomerID
 WHERE
  Customer.CustomerID IN (11000, 11001)
 ORDER BY
  Customer.CustomerID
 FOR XML AUTO, ROOT('Orders'), XMLSCHEMA

The output looks like this:

<Orders>
  <xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet4"
xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet4" xmlns:xsd="http://www.w3.org/2001/
XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes"
elementFormDefault="qualified">
    <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes"
schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
    <xsd:element name="Customer">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element ref="schema:OrderDetail" minOccurs="0" maxOccurs="unbounded" /
>
        </xsd:sequence>
        <xsd:attribute name="CustomerID" type="sqltypes:int" use="required" />
      </xsd:complexType>
    </xsd:element>
    <xsd:element name="OrderDetail">
      <xsd:complexType>
        <xsd:attribute name="SalesOrderID" type="sqltypes:int" use="required" />
        <xsd:attribute name="OrderDate" type="sqltypes:datetime" use="required" />
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <Customer xmlns="urn:schemas-microsoft-com:sql:SqlRowSet4" CustomerID="11000">
    <OrderDetail SalesOrderID="43793" OrderDate="2005-07-22T00:00:00" />
    <OrderDetail SalesOrderID="51522" OrderDate="2007-07-22T00:00:00" />
    <OrderDetail SalesOrderID="57418" OrderDate="2007-11-04T00:00:00" />
  </Customer>
  <Customer xmlns="urn:schemas-microsoft-com:sql:SqlRowSet4" CustomerID="11001">
    <OrderDetail SalesOrderID="43767" OrderDate="2005-07-18T00:00:00" />
    <OrderDetail SalesOrderID="51493" OrderDate="2007-07-20T00:00:00" />
    <OrderDetail SalesOrderID="72773" OrderDate="2008-06-12T00:00:00" />
  </Customer>
</Orders>

SQL Server infers the schema based on the underlying data types of the result set. For example, the SalesOrderID field is set to an int and is a required field (as per the inline schema based on the properties of the field in the underlying SQL table).

5. Producing Element-Based XML

Element-based XML is more verbose than attribute-based XML but is usually easier to view and work with. Initially, in SQL Server 2000, FOR XML RAW and FOR XML AUTO could only generate attribute-based XML .

Both FOR XML RAW and FOR XML AUTO were later enhanced to support the ELEMENTS keyword, enabling them to alternatively produce element-based XML rather than attribute-based XML. When all you need is element-based XML, and you require no other customization over the shape of generated XML, you will find it much easier to use FOR XML RAW/AUTO with ELEMENT rather than FOR XML EXPLICIT (and even FOR XML PATH). Example 6 demonstrates this.

Example 6. Using FOR XML AUTO with ELEMENTS to produce element-based hierarchical XML.

SELECT
  Customer.CustomerID,
  OrderDetail.SalesOrderID,
  OrderDetail.OrderDate
 FROM
  Sales.Customer AS Customer
   INNER JOIN Sales.SalesOrderHeader AS OrderDetail
    ON OrderDetail.CustomerID = Customer.CustomerID
 WHERE
  Customer.CustomerID IN (11000, 11001)
 ORDER BY
  Customer.CustomerID
 FOR XML AUTO, ROOT('Orders'), ELEMENTS

Here are the query results:

<Orders>
  <Customer>
    <CustomerID>11000</CustomerID>
    <OrderDetail>
      <SalesOrderID>43793</SalesOrderID>
      <OrderDate>2005-07-22T00:00:00</OrderDate>
    </OrderDetail>
    <OrderDetail>
      <SalesOrderID>51522</SalesOrderID>
      <OrderDate>2007-07-22T00:00:00</OrderDate>
    </OrderDetail>
    <OrderDetail>
      <SalesOrderID>57418</SalesOrderID>
      <OrderDate>2007-11-04T00:00:00</OrderDate>
    </OrderDetail>
  </Customer>
  <Customer>
    <CustomerID>11001</CustomerID>
    <OrderDetail>
      <SalesOrderID>43767</SalesOrderID>
      <OrderDate>2005-07-18T00:00:00</OrderDate>
    </OrderDetail>
    <OrderDetail>
      <SalesOrderID>51493</SalesOrderID>
      <OrderDate>2007-07-20T00:00:00</OrderDate>
    </OrderDetail>
    <OrderDetail>
      <SalesOrderID>72773</SalesOrderID>
      <OrderDate>2008-06-12T00:00:00</OrderDate>
    </OrderDetail>
  </Customer>
</Orders>

You can see that each column of the query becomes a nested element in the resulting XML, as opposed to an attribute of one single element. The ELEMENTS keyword used in conjunction with FOR XML RAW or FOR XML AUTO converts each column from your result set to an individual XML element. FOR XML AUTO also converts each row from a joined table to a new XML element, as just demonstrated.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us