Creating CDATA Sections Using sql:use-cdata (XDR Schema)

Important

This topic is included as a reference for legacy applications. No future development work will be done on this feature. Avoid using this feature in new development work. Instead, use annotated XSD schemas to create XML views. For more information, see Introduction to Annotated XSD Schemas (SQLXML 4.0). You can convert existing annotated XDR schemas to XSD schemas. For more information, see Converting Annotated XDR Schemas to Equivalent XSD Schemas (SQLXML 4.0).

In XML, CDATA sections are used to escape blocks of text containing characters that would otherwise be recognized as markup.

Microsoft SQL Server data may contain characters that are considered special by the XML parser, for example, characters such as <, >, <=, & are treated as markup characters. If you want to avoid SQL Server data containing special characters being treated as markup, you can wrap them in a CDATA section. The text placed in the CDATA section is treated as plain text.

The sql:use-cdata annotation is used specify if the data returned by SQL Server be wrapped in a CDATA section. Use sql:use-cdata annotation to indicate if the value from the column specified by sql:field should be enclosed in a CDATA section. The sql:use-cdata annotation can be specified on <ElementType> or <element>, and takes a Boolean value (0 = FALSE, 1 = TRUE). sql:use-cdata cannot be used with sql:url-encode or on any of the attribute types ID, IDREF, IDREFS, NMTOKEN, or NMTOKENS.

Examples

To create working samples using the following examples, you must meet certain requirements. For more information, see Requirements for Running SQLXML Examples.

A. Specify sql:use-cdata on an element

In this schema, sql:use-cdata is set to 1 (TRUE) for the <AddressLine1> element. As a result, the data for <AddressLine1> is returned in the CDATA section.

<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
   xmlns:dt="urn:schemas-microsoft-com:datatypes"
   xmlns:sql="urn:schemas-microsoft-com:xml-sql">

   <ElementType name="AddressID" content="textOnly" />
   <ElementType name="AddressLine1" content="textOnly" />

   <ElementType name="Address" sql:relation="Person.Address">
      <element type="AddressID" />
      <element type="AddressLine1" sql:use-cdata="1"  />
   </ElementType>

</Schema>

To test a sample XPath query against the schema

  1. Copy the schema code above and paste it into a text file. Save the file as UseCdataXdr.xml.

  2. Copy the following template and paste it into a text file. Save the file as UseCdataXdrT.xml in the same directory where you saved UseCdataXdr.xml.

    <ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
      <sql:xpath-query mapping-schema="UseCdataXdr.xml">
        /Address[@AddressID=1]
      </sql:xpath-query>
    </ROOT>
    

    The directory path specified for the mapping schema (UseCdataXdr.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example:

    mapping-schema="C:\MyDir\UseCdataXdr.xml"
    
  3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template.

    For more information, see Using ADO to Execute SQLXML 4.0 Queries.

Here is the result set:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
  <Address>
    <AddressID>1</AddressID> 
    <AddressLine1>
       <![CDATA[ 1970 Napa Ct.]]> 
    </AddressLine1>
  </Address>
</ROOT>

See Also

Reference

Using XPath Queries in SQLXML 4.0

Help and Information

Getting SQL Server 2005 Assistance