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="