Sunday, April 13, 2014

FOR XML RAW with XMLSCHEMA Directive

Today, we'll explore the XMLSCHEMA directive of the FOR XML clause.

XMLSCHEMA Directive
     Return XSD (XML Schema Definition) schema along with XML data of XML document/fragment.
     Can only use with RAW and AUTO mode.

SELECT  custid, contactname, fax
FROM    Sales.Customers
WHERE   custid <= 3
FOR XML RAW('Customer')
      , ELEMENTS XSINIL
      , ROOT('Customers')
      , XMLSCHEMA;

Result:
Notice the targetNamespce contains an Uniform Resource Name(URN) with format as: schemas-microsoft-com:sql:SqlRowSetn. Every time we execute the same query above in the same query window, n is incremented by 1. The sqltypes namespace is the link to SQL Server datatype to XSD type mapping.
 
<Customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" 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 name="custid" type="sqltypes:int" nillable="1" />
          <xsd:element name="contactname" nillable="1">
            <xsd:simpleType>
              <xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                <xsd:maxLength value="30" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          <xsd:element name="fax" nillable="1">
            <xsd:simpleType>
              <xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                <xsd:maxLength value="24" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <Customer xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
    <custid>1</custid>
    <contactname>Allen, Michael</contactname>
    <fax>030-0123456</fax>
  </Customer>
  <Customer xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
    <custid>2</custid>
    <contactname>Hassall, Mark</contactname>
    <fax>(5) 456-7890</fax>
  </Customer>
  <Customer xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
    <custid>3</custid>
    <contactname>Peoples, John</contactname>
    <fax xsi:nil="true" />
  </Customer>
</Customers>

There're a lot more information about XSD. You can read more about XSD here.

Reference:
http://technet.microsoft.com/en-us/library/bb510461.aspx
http://technet.microsoft.com/en-us/library/ms175113.aspx
http://www.w3.org/TR/xmlschema11-1/

No comments:

Post a Comment