Using XML Templates

XML and Internet Support

XML and Internet Support

Using XML Templates

This general form for a template shows the way SQL queries and XPath queries are specified:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"
       sql:xsl='XSL FileName' >
  <sql:header>
    <sql:param>..</sql:param>
    <sql:param>..</sql:param>...n
  </sql:header>
  <sql:query>
    sql statement(s)
  </sql:query>
  <sql:xpath-query mapping-schema="SchemaFileName.xml">
    XPath query
  </sql:xpath-query>
</ROOT>

Everything in a template is optional. The elements <header>, <param>, <query>, <XPath-query>, and the attribute mapping-schema are defined in the sql namespace. Therefore, the namespace declaration xmlns:sql="urn:schemas-microsoft-com:xml-sql" is required. The namespace can be named anything; sql is just an alias.

<ROOT>

This tag is specified to provide a single top-level element (also referred as root tag) for the resulting XML document. The <ROOT> tag can have any name.

<sql:header>

This tag is used to hold any header values. In the current implementation, only the <sql:param> element can be specified in this tag. The <sql:header> tag acts as containing tag, allowing you to define multiple parameters. With all the parameter definitions in one place, processing the parameter definitions is more efficient.

<sql:param>

This element is used to define a parameter that is passed to the query inside the template. Each <param> element defines one parameter. Multiple <param> elements can be specified in the <sql:header> tag.

<sql:query>

This element is used to specify SQL queries. You can specify multiple <sql:query> elements in a template.

<sql:xpath-query>

This element is used to specify an XPath query. Because the XPath query is executed against the annotated XML-Data Reduced (XDR) schema, the schema file name must be specified using the mapping-schema attribute.

sql:xsl

This attribute is used to specify an Extensible Stylesheet Language (XSL) style sheet that will be applied to the resulting XML document. In specifying the XSL file, a relative or an absolute path can be specified. This relative path specified is relative to the directory associated with the virtual name of template type. For example, if the directory associated with the virtual name of template type is C:\Template, then the relative path, Xyz/MyXSL.xml specified for sql:xsl maps to C:\Template\Xyz\MyXSL.xml.

mapping-schema

This attribute is used to identify the annotated XDR schema. This attribute is specified only if you are executing an XPath query in the template. The XPath query is executed against the annotated XDR schema. In specifying the mapping schema file, a relative or an absolute path can be specified. This relative path specified is relative to the directory associated with the virtual name of template type. For example, if the directory associated with the virtual name of template type is C:\Template, then the relative path, Schema/MSchema.xml specified for mapping-schema maps to C:\Template\Schema\MSchema.xml.

Note  Each <sql:query> or <sql:XPath-query> represents a separate transaction. Therefore, if you have multiple <sql:query> or <sql:XPath-query> tags in the template, and if one fails, the others will proceed.

If contenttype is set, Sqlisapi.dll returns that header information to the browser. If the contenttype is not set, the first character in the template file is used by the urlmon to determine the content-type. If the first character in the template is the < character or a Unicode byte order mark (0xFFFE), text/xml is returned to the browser as the content-type, and the browser displays the result. Otherwise, Sqlisapi.dll does not send the content-type header information that instructs the browser on how to display the result; therefore, you do not see the result in the browser.

Before templates can be specified in the URL, the virtual name of template type must be created using the IIS Virtual Directory Management for SQL Server utility. For more information, see Using IIS Virtual Directory Management for SQL Server Utility.

Storing the Templates

The template is stored in the directory associated with the virtual name of template type or one its subdirectories:

  • If the template is stored in the directory associated with virtual name of template type, the URL query looks like:
    http://IISServer/nwind/TemplateVirtualName/TemplateFile.xml
    
  • If the template is stored in the subdirectory associated with virtual name of template type (xyz), the URL query looks like:
    http://IISServer/nwind/TemplateVirtualName/xyz/TemplateFile.xml
    

See Also

Using IIS Virtual Directory Management for SQL Server Utility

Accessing SQL Server Using HTTP

Retrieving XML Documents Using FOR XML

Using XPath Queries