使用 xml 模板
下面是模板的一般形式,显示了指定 sql 查询和 xpath 查询的方式:
<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>
模板中的任何元素都是可选的。元素 <header>、<param>、<query>、<xpath-query> 和特性映射架构在 sql 命名空间中定义。因此,必须在命名空间中声明 xmlns:sql="urn:schemas-microsoft-com:xml-sql"。可对命名空间任意命名;sql 只是一个别名。
<root>
指定此标记以提供所得到的 xml 文档的单个顶层元素(也称为根标记)。<root> 标记可以是任何名称。
<sql:header>
此标记用于保存所有标题值。在目前的实施方案中,只能在此标记中指定 <sql:param> 元素。<sql:header> 用作包含标记,使您得以定义多个参数。所有的参数定义都在一个位置中,这使得处理参数定义更有效。
<sql:param>
该元素用于定义在模板内传递到查询的参数。每个 <param> 元素定义一个参数。可以在 <sql:header> 标记中定义多个 <param> 元素。
<sql:query>
该元素用于指定 sql 查询。可以在模板中指定多个 <sql:query> 元素。
<sql:xpath-query>
该元素用于指定 xpath 查询。因为 xpath 查询是在带批注的 xml 数据简化 (xdr) 架构上执行,所以必须使用 mapping-schema 特性指定架构文件名。
sql:xsl
该特性用于指定将应用于所得到的 xml 文档的可扩展样式表语言 (xsl) 样式表。在指定映射架构文件时,可以指定相对或绝对路径。指定的相对路径是相对于与 template 类型的虚拟名称关联的目录。例如,如果与 template 类型的虚拟名称相关联的目录是 c:\template,那么为 sql:xsl 指定的相对路径 xyz/myxsl.xml 将映射到 c:\template\xyz\myxsl.xml。
mapping-schema
该特性用于标识带批注的 xdr 架构。仅当在模板中执行 xpath 查询时才指定该特性。xpath 查询在带批注的 xdr 架构上执行。在指定映射架构文件时,可以指定相对或绝对路径。指定的相对路径是相对于与 template 类型的虚拟名称关联的目录。例如,如果与 template 类型的虚拟名称关联的目录是 c:\template,则相对路径(为 mapping-schema 指定的 schema/mschema.xml)映射到 c:\template\schema\mschema.xml。
说明 每个 <sql:query> 或 <sql:xpath-query> 表示单独的事务。因此,如果在模板中有多个 <sql:query> 或 <sql:xpath-query> 标记,当一个标记失败时,其它标记将继续。
如果设置了 contenttype,则 sqlisapi.dll 将标题信息返回给浏览器。如果没有设置 contenttype,则 urlmon 使用模板文件中的第一个字符确定内容类型。如果模板中的第一个字符是 < 字符或 unicode 字节顺序标志 (0xfffe),则将 text/xml 作为内容类型返回给浏览器,且浏览器显示结果。否则,sqlisapi.dll 不发送用以指导浏览器如何显示结果的内容类型标题信息;因此,在浏览器中将看不到结果。
可以在 url 模板中指定模板之前,必须使用用于 sql server 的 iis 虚拟目录管理实用工具创建 template 类型的虚拟名称。有关更多信息,请参见使用用于 sql server 的 iis 虚拟目录管理实用工具。
存储模板
模板存储在与 template 类型的虚拟名称关联的目录或它的一个子目录中:
如果模板存储在与 template 类型的虚拟名称关联的目录中,则 url 查询的形式如下:
http://iisserver/nwind/templatevirtualname/templatefile.xml
如果模板存储在与 template 类型 (xyz) 的虚拟名称关联的子目录中,则 url 查询的形式如下:
http://iisserver/nwind/templatevirtualname/xyz/templatefile.xml在模板中指定 xsl 样式表可以对查询结果应用可扩展样式表语言 (xsl) 样式表。当使用 http 执行模板时,可以用下列方法指定 xsl 文件: 在模板中使用 sql:xsl 特性。使用 xsl 关键字作为 url 的一部分,以指定将用于处理所得到的 xml 数据的 xsl 文件。 如果在模板中使用 sql:xsl 指定 xsl 文件并且在 url 中使用关键字 xsl 指定 xsl 文件,则先对结果应用在模板中指定的 xsl 样式表,然后应用在 url 中指定的 xsl 文件。示例在下列示例中,nwind 是使用用于 sql server 的 iis 虚拟目录管理实用工具创建的虚拟目录,template 是创建虚拟目录时定义的 template 类型的虚拟名称(创建虚拟目录时可以为其指定任何虚拟名称)。有关更多信息,请参见使用用于 sql server 的 iis 虚拟目录管理实用工具。a. 在模板中指定 sql:xsl 以处理结果在下例中,模板包含简单的 select 语句。按照使用 sql:xsl 指定的 xsl 文件中的说明处理查询结果。<?xml version =1.0 encoding=utf-8?>
<root xmlns:sql=urn:schemas-microsoft-com:xml-sql
sql:xsl=myxsl.xsl>
<sql:query>
select firstname, lastname from employees for xml auto
</sql:query>
</root>
为举例说明,将模板 (templatewithxsl.xml) 存储在与 template 类型的虚拟名称 (template) 关联的目录中。xsl 文件 (myxsl.xsl) 也存储在同一目录中。下面是 xsl 文件:<?xml version=1.0 encoding=utf-8?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">
<xsl:template match = employees>
<tr>
<td><xsl:value-of select = @firstname /></td>
<td><b><xsl:value-of select = @lastname /></b></td>
</tr>
</xsl:template>
<xsl:template match = />
<html>
<head>
<style>th { background-color: #cccccc }</style>
</head>
<body>
<table border=1 style=width:300;>
<tr><th colspan=2>employees</th></tr>
<tr><th >first name</th><th>last name</th></tr>
<xsl:apply-templates select = root />
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
下面的 url 执行模板:http://iisserver/nwind/template/templatewithxsl.xml?contenttype=text/html结果以两列表的格式(firstname 和 lastname)显示。还可在 url 中指定 xsl 文件,而不是在模板中(使用 sql:xsl)指定该文件。在这种情况下,xsl 文件必须存储在与虚拟根 (nwind) 或其子目录关联的目录中,此时必须在 url 中指定相对路径。假设 xsl 文件存储在与 nwind 虚拟目录关联的目录中,则下面的 url 执行模板:http://iisserver/nwind/template/templatefile.xml?xsl=myxsl.xsl
如果 xsl 文件存储在虚拟根目录的一个子目录 (x) 中,则下面的 url 这样指定相对路径:http://iisserver/nwind/template/templatefile.xml?xsl=/x/myxsl.xsl
如果在模板中使用 sql:xsl 并且在 url 中使用关键字 xsl 指定 xsl 文件,则在模板中指定的 xsl 样式表首先应用于结果,然后应用在 url 中指定的 xsl 文件。
示例 xpath 查询
本示例 xpath 查询引用下面两个映射架构。映射架构是带批注的简化 xml-data (xdr) 架构。有关映射架构的更多信息,请参见使用带批注的 xdr 架构创建 xml 视图。
说明 在可以使用 url 执行示例 xpath 查询前,必须创建虚拟根以访问 northwind 数据库和 template 及 schema 类型的虚拟名称。有关创建示例 nwind 虚拟目录和虚拟名称的信息,请参见创建 nwind 虚拟目录。有关使用 http 访问 microsoft® sql server™ 的更多信息,请参见使用 http 访问 sql server。
有两种方法可对带批注的 xdr 架构执行 xpath 查询:
创建一个模板,在其中包含一个 xpath 查询。然后在 url(例如 http://iisserver/virtualroot/templatevirtualname/templatefile.xml)中执行该模板。在该模板中,指定将对其执行 xpath 查询的映射架构。在这种情况下,必须将映射架构存储到与 template 类型的虚拟名称相关联的目录(或它的一个子目录,此时将某相对路径指定为模板内 mapping-schema 特性的值)下。
可直接在 url(例如 http://iisserver/virtualroot/schemavirtualname/schemafile.xml/xpathquery)中指定 xpath 查询。在这种情况下,必须将架构文件存储到与 schema 类型的虚拟名称相关联的目录下。
示例带批注的 xdr 架构
在本节的所有示例中,为了说明,将在模板中指定 xpath 查询,并使用 http 执行该模板。因此,必须使用此映射架构文件 (sampleschema1.xml),它保存在与 template 类型虚拟名称相关联的目录下:
<?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="customer" sql:relation="customers">
<attributetype name="customerid" dt:type="id" />
<attributetype name="companyname" />
<attributetype name="contactname" />
<attributetype name="city" />
<attributetype name="fax" />
<attributetype name="orders" dt:type="idrefs" sql:id-prefix="ord-" />
<attribute type="customerid" />
<attribute type="companyname" />
<attribute type="contactname" />
<attribute type="city" />
<attribute type="fax" />
<attribute type="orders" sql:relation="orders" sql:field="orderid">
<sql:relationship
key-relation="customers"
key="customerid"
foreign-relation="orders"
foreign-key="customerid" />
</attribute>
<element type="order">
<sql:relationship
key-relation="customers"
key="customerid"
foreign-relation="orders"
foreign-key="customerid" />
</element>
</elementtype>
<elementtype name="order" sql:relation="orders">
<attributetype name="orderid" dt:type="id" sql:id-prefix="ord-" />
<attributetype name="employeeid" />
<attributetype name="orderdate" />
<attributetype name="requireddate" />
<attributetype name="shippeddate" />
<attribute type="orderid" />
<attribute type="employeeid" />
<attribute type="orderdate" />
<attribute type="requireddate" />
<attribute type="shippeddate" />
<element type="orderdetail">
<sql:relationship
key-relation="orders"
key="orderid"
foreign-relation="[order details]"
foreign-key="orderid" />
</element>
</elementtype>
<elementtype name="orderdetail" sql:relation="[order details]"
sql:key-fields="orderid productid">
<attributetype name="productid" dt:type="idref"
sql:id-prefix="prod-" />
<attributetype name="unitprice"/>
<attributetype name="quantity" />
<attribute type="productid" />
<attribute type="unitprice" sql:field="unitprice" />
<attribute type="quantity" />
<element type="discount" sql:field="discount"/>
</elementtype>
<elementtype name="discount" dt:type="string"
sql:relation="[order details]"/>
<elementtype name="employee" sql:relation="employees">
<attributetype name="employeeid" />
<attributetype name="lastname" />
<attributetype name="firstname" />
<attributetype name="title" />
<attribute type="employeeid" />
<attribute type="lastname" />
<attribute type="firstname" />
<attribute type="title" />
</elementtype>
</schema>
说明
按查询所执行的 xpath 操作类型将示例查询分组。
使用 xml 模板
下面是模板的一般形式,显示了指定 sql 查询和 xpath 查询的方式:
<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>
模板中的任何元素都是可选的。元素 <header>、<param>、<query>、<xpath-query> 和特性映射架构在 sql 命名空间中定义。因此,必须在命名空间中声明 xmlns:sql="urn:schemas-microsoft-com:xml-sql"。可对命名空间任意命名;sql 只是一个别名。
<root>
指定此标记以提供所得到的 xml 文档的单个顶层元素(也称为根标记)。<root> 标记可以是任何名称。
<sql:header>
此标记用于保存所有标题值。在目前的实施方案中,只能在此标记中指定 <sql:param> 元素。<sql:header> 用作包含标记,使您得以定义多个参数。所有的参数定义都在一个位置中,这使得处理参数定义更有效。
<sql:param>
该元素用于定义在模板内传递到查询的参数。每个 <param> 元素定义一个参数。可以在 <sql:header> 标记中定义多个 <param> 元素。
<sql:query>
该元素用于指定 sql 查询。可以在模板中指定多个 <sql:query> 元素。
<sql:xpath-query>
该元素用于指定 xpath 查询。因为 xpath 查询是在带批注的 xml 数据简化 (xdr) 架构上执行,所以必须使用 mapping-schema 特性指定架构文件名。
sql:xsl
该特性用于指定将应用于所得到的 xml 文档的可扩展样式表语言 (xsl) 样式表。在指定映射架构文件时,可以指定相对或绝对路径。指定的相对路径是相对于与 template 类型的虚拟名称关联的目录。例如,如果与 template 类型的虚拟名称相关联的目录是 c:\template,那么为 sql:xsl 指定的相对路径 xyz/myxsl.xml 将映射到 c:\template\xyz\myxsl.xml。
mapping-schema
该特性用于标识带批注的 xdr 架构。仅当在模板中执行 xpath 查询时才指定该特性。xpath 查询在带批注的 xdr 架构上执行。在指定映射架构文件时,可以指定相对或绝对路径。指定的相对路径是相对于与 template 类型的虚拟名称关联的目录。例如,如果与 template 类型的虚拟名称关联的目录是 c:\template,则相对路径(为 mapping-schema 指定的 schema/mschema.xml)映射到 c:\template\schema\mschema.xml。
说明 每个 <sql:query> 或 <sql:xpath-query> 表示单独的事务。因此,如果在模板中有多个 <sql:query> 或 <sql:xpath-query> 标记,当一个标记失败时,其它标记将继续。
如果设置了 contenttype,则 sqlisapi.dll 将标题信息返回给浏览器。如果没有设置 contenttype,则 urlmon 使用模板文件中的第一个字符确定内容类型。如果模板中的第一个字符是 < 字符或 unicode 字节顺序标志 (0xfffe),则将 text/xml 作为内容类型返回给浏览器,且浏览器显示结果。否则,sqlisapi.dll 不发送用以指导浏览器如何显示结果的内容类型标题信息;因此,在浏览器中将看不到结果。
可以在 url 模板中指定模板之前,必须使用用于 sql server 的 iis 虚拟目录管理实用工具创建 template 类型的虚拟名称。有关更多信息,请参见使用用于 sql server 的 iis 虚拟目录管理实用工具。
存储模板
模板存储在与 template 类型的虚拟名称关联的目录或它的一个子目录中:
如果模板存储在与 template 类型的虚拟名称关联的目录中,则 url 查询的形式如下:
http://iisserver/nwind/templatevirtualname/templatefile.xml
如果模板存储在与 template 类型 (xyz) 的虚拟名称关联的子目录中,则 url 查询的形式如下:
http://iisserver/nwind/templatevirtualname/xyz/templatefile.xml在模板中指定 xsl 样式表可以对查询结果应用可扩展样式表语言 (xsl) 样式表。当使用 http 执行模板时,可以用下列方法指定 xsl 文件: 在模板中使用 sql:xsl 特性。使用 xsl 关键字作为 url 的一部分,以指定将用于处理所得到的 xml 数据的 xsl 文件。 如果在模板中使用 sql:xsl 指定 xsl 文件并且在 url 中使用关键字 xsl 指定 xsl 文件,则先对结果应用在模板中指定的 xsl 样式表,然后应用在 url 中指定的 xsl 文件。示例在下列示例中,nwind 是使用用于 sql server 的 iis 虚拟目录管理实用工具创建的虚拟目录,template 是创建虚拟目录时定义的 template 类型的虚拟名称(创建虚拟目录时可以为其指定任何虚拟名称)。有关更多信息,请参见使用用于 sql server 的 iis 虚拟目录管理实用工具。a. 在模板中指定 sql:xsl 以处理结果在下例中,模板包含简单的 select 语句。按照使用 sql:xsl 指定的 xsl 文件中的说明处理查询结果。<?xml version =1.0 encoding=utf-8?>
<root xmlns:sql=urn:schemas-microsoft-com:xml-sql
sql:xsl=myxsl.xsl>
<sql:query>
select firstname, lastname from employees for xml auto
</sql:query>
</root>
为举例说明,将模板 (templatewithxsl.xml) 存储在与 template 类型的虚拟名称 (template) 关联的目录中。xsl 文件 (myxsl.xsl) 也存储在同一目录中。下面是 xsl 文件:<?xml version=1.0 encoding=utf-8?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">
<xsl:template match = employees>
<tr>
<td><xsl:value-of select = @firstname /></td>
<td><b><xsl:value-of select = @lastname /></b></td>
</tr>
</xsl:template>
<xsl:template match = />
<html>
<head>
<style>th { background-color: #cccccc }</style>
</head>
<body>
<table border=1 style=width:300;>
<tr><th colspan=2>employees</th></tr>
<tr><th >first name</th><th>last name</th></tr>
<xsl:apply-templates select = root />
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
下面的 url 执行模板:http://iisserver/nwind/template/templatewithxsl.xml?contenttype=text/html结果以两列表的格式(firstname 和 lastname)显示。还可在 url 中指定 xsl 文件,而不是在模板中(使用 sql:xsl)指定该文件。在这种情况下,xsl 文件必须存储在与虚拟根 (nwind) 或其子目录关联的目录中,此时必须在 url 中指定相对路径。假设 xsl 文件存储在与 nwind 虚拟目录关联的目录中,则下面的 url 执行模板:http://iisserver/nwind/template/templatefile.xml?xsl=myxsl.xsl
如果 xsl 文件存储在虚拟根目录的一个子目录 (x) 中,则下面的 url 这样指定相对路径:http://iisserver/nwind/template/templatefile.xml?xsl=/x/myxsl.xsl
如果在模板中使用 sql:xsl 并且在 url 中使用关键字 xsl 指定 xsl 文件,则在模板中指定的 xsl 样式表首先应用于结果,然后应用在 url 中指定的 xsl 文件。
示例 xpath 查询
本示例 xpath 查询引用下面两个映射架构。映射架构是带批注的简化 xml-data (xdr) 架构。有关映射架构的更多信息,请参见使用带批注的 xdr 架构创建 xml 视图。
说明 在可以使用 url 执行示例 xpath 查询前,必须创建虚拟根以访问 northwind 数据库和 template 及 schema 类型的虚拟名称。有关创建示例 nwind 虚拟目录和虚拟名称的信息,请参见创建 nwind 虚拟目录。有关使用 http 访问 microsoft® sql server™ 的更多信息,请参见使用 http 访问 sql server。
有两种方法可对带批注的 xdr 架构执行 xpath 查询:
创建一个模板,在其中包含一个 xpath 查询。然后在 url(例如 http://iisserver/virtualroot/templatevirtualname/templatefile.xml)中执行该模板。在该模板中,指定将对其执行 xpath 查询的映射架构。在这种情况下,必须将映射架构存储到与 template 类型的虚拟名称相关联的目录(或它的一个子目录,此时将某相对路径指定为模板内 mapping-schema 特性的值)下。
可直接在 url(例如 http://iisserver/virtualroot/schemavirtualname/schemafile.xml/xpathquery)中指定 xpath 查询。在这种情况下,必须将架构文件存储到与 schema 类型的虚拟名称相关联的目录下。
示例带批注的 xdr 架构
在本节的所有示例中,为了说明,将在模板中指定 xpath 查询,并使用 http 执行该模板。因此,必须使用此映射架构文件 (sampleschema1.xml),它保存在与 template 类型虚拟名称相关联的目录下:
<?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="customer" sql:relation="customers">
<attributetype name="customerid" dt:type="id" />
<attributetype name="companyname" />
<attributetype name="contactname" />
<attributetype name="city" />
<attributetype name="fax" />
<attributetype name="orders" dt:type="idrefs" sql:id-prefix="ord-" />
<attribute type="customerid" />
<attribute type="companyname" />
<attribute type="contactname" />
<attribute type="city" />
<attribute type="fax" />
<attribute type="orders" sql:relation="orders" sql:field="orderid">
<sql:relationship
key-relation="customers"
key="customerid"
foreign-relation="orders"
foreign-key="customerid" />
</attribute>
<element type="order">
<sql:relationship
key-relation="customers"
key="customerid"
foreign-relation="orders"
foreign-key="customerid" />
</element>
</elementtype>
<elementtype name="order" sql:relation="orders">
<attributetype name="orderid" dt:type="id" sql:id-prefix="ord-" />
<attributetype name="employeeid" />
<attributetype name="orderdate" />
<attributetype name="requireddate" />
<attributetype name="shippeddate" />
<attribute type="orderid" />
<attribute type="employeeid" />
<attribute type="orderdate" />
<attribute type="requireddate" />
<attribute type="shippeddate" />
<element type="orderdetail">
<sql:relationship
key-relation="orders"
key="orderid"
foreign-relation="[order details]"
foreign-key="orderid" />
</element>
</elementtype>
<elementtype name="orderdetail" sql:relation="[order details]"
sql:key-fields="orderid productid">
<attributetype name="productid" dt:type="idref"
sql:id-prefix="prod-" />
<attributetype name="unitprice"/>
<attributetype name="quantity" />
<attribute type="productid" />
<attribute type="unitprice" sql:field="unitprice" />
<attribute type="quantity" />
<element type="discount" sql:field="discount"/>
</elementtype>
<elementtype name="discount" dt:type="string"
sql:relation="[order details]"/>
<elementtype name="employee" sql:relation="employees">
<attributetype name="employeeid" />
<attributetype name="lastname" />
<attributetype name="firstname" />
<attributetype name="title" />
<attribute type="employeeid" />
<attribute type="lastname" />
<attribute type="firstname" />
<attribute type="title" />
</elementtype>
</schema>
说明
按查询所执行的 xpath 操作类型将示例查询分组。
