服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

深入探讨sql server 2000对xml的支持(四)


  xpath查询 架构和模板

  xpath查询也可以被内嵌进一个xml模板文件中,下面的代码是一个包含xpath查询的简单的xml模板文件。

<northwind xmlns:sql=
"urn:schemas-microsoft-com:
xml-sql">
<sql:xpath-query mapping-schema=
"file4.xdr">
/customer[@customerid=
'alfki']/order
</sql:xpath-query>
</northwind>


  这个查询使用了一个架构(schema)返回customerid号为alfki的用户的所有的订单,如果想要使xpath语句运行,必须使用一个xdr架构文件映射不同的xml元素和属性到相应的数据库表和字段名。下面给出了这个架构文件。

<?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>
<element type="employee">
<sql:relationship
key-relation="orders"
key="employeeid"
foreign-relation="employees"
foreign-key="employeeid" />
</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"/>
<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" dt:type="idref"
sql:id-prefix="emp-" />
<attributetype name="lastname" />
<attributetype name="firstname" />
<attributetype name="title" />
<attribute type="employeeid"/>

<attribute type="lastname" />
<attribute type="firstname" />
<attribute type="title" />
</elementtype>
</schema>


  如果您想深入了解架构文件的话,请参看sql server 2000的用户文档或等待我的以后的文章。

  和内嵌在xml模板文件中的sql查询语句一样,xpath查询语句使用urn:schemas-microsoft-com:xml-sql和sql作为前缀,共同标示用在模板中的自定义元素和属性,对于xpath查询而言,我们使用一个名为xpath-query的元素来标识查询语法,这个元素也有一个名为mapping-schema的属性,用来标示相应用以映射表和字段到特定的xml项目的架构文件所在的路径。

  下面的代码给出了另一个使用更复杂xpath查询的模板文件。


<northwind xmlns:sql=
"urn:schemas-microsoft-com:xml-sql">
<sql:xpath-query mapping-schema=
"listing4.xdr">
/customer[@customerid=
'alfki']/order/
employee[@lastname='suyama']
</sql:xpath-query>
</northwind>


  当执行这个模板文件时,xpath查询返回与某个客户签下订单的雇员(employee)的姓名,结果如下:

<northwind xmlns:sql=
"urn:schemas-microsoft-com:xml-sql">
<employee employeeid="emp-6"
lastname="suyama"
firstname="michael"
title="sales
representative"/>
</northwind>


  模板文件中使用的xpath查询也可以使用参数,处理过程很象在xsl样式表中使用参数一样。象xsl一样,使用$指定一个变量。下面的代码说明了如何在一个包含xpath查询的模板文件中整合变量。


<northwind xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:header>
<sql:param name="id"/>
</sql:header>
<sql:xpath-query mapping-schema="listing4.xdr"> {{should this be "listing6.xdr"?}}
/customer/order[@orderid=$id]
</sql:xpath-query>
</northwind>


  通过在url中传递参数名和相应的参数值,我们就可以完成把参数传递到模板中的操作。结果如下:


<northwind xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<order orderid="ord-10643" employeeid=
"6" orderdate="1997-08-25t00:00:00" requireddate=
"1997-09-22t00:00:00" shippeddate=
"1997-09-02t00:00:00">
<employee employeeid="emp-6" lastname=
"suyama" firstname="michael" title=
"sales representative" />
<orderdetail productid="prod-28" unitprice=
"45.6" quantity="15">
<discount>0.25</discount>
</orderdetail>
<orderdetail productid="prod-39" unitprice=
"18" quantity="21">
<discount>0.25</discount>
</orderdetail>
<orderdetail productid="prod-46" unitprice=
"12" quantity="2">
<discount>0.25</discount>
</orderdetail>
</order>
</northwind>



  小结

  通过使用上面我介绍的几种技术,我们可以直接从sql server 2000数据库中直接取得xml数据。如我所介绍,url查询、xml模板文件、xdr架构和xpath查询提供了强大的功能,从sql server 2000中直接获得xml数据。除此之外,还有很多重要的概念,由于篇幅有限在本文中不可能详述,如for xml explicit查询和openxml这些技术,我会在以后的文章中进一步进行讨论,请大家等待。

扫描关注微信公众号