在 XPath 查詢中指定軸(轉自MSSQL手冊)
來源:互聯網
上載者:User
sql 以下樣本顯示如何在 XPath 查詢中指定軸。這些樣本中的 XPath 查詢都在 SampleSchema1.xml 中所包含的映射架構上指定。有關此樣本架構的資訊,請參見樣本 XPath 查詢。
樣本
A. 檢索上下文節點的子項目
此 XPath 查詢選定上下文節點的所有 <Customer> 子項目:
/child::Employee
在此查詢中,child 是軸,Customer 是節點測試(如果 Customer 是 <element> 節點,則該測試為 TRUE,因為 <element> 是與 child 軸相關聯的主要節點類型)。
child 是預設軸。因此,可將該查詢編寫為:
/Employee
在映射架構上測試 XPath 查詢
建立下面的模板 (MyTemplate.xml) 並將其儲存在與 template 虛擬名稱相關聯的目錄中。
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:xpath-query mapping-schema="SampleSchema2.xml">
/Employee
</sql:xpath-query>
</ROOT>
下面的 URL 執行模板:
http://IISServer/VirtualRoot/template/MyTemplate.xml
可直接在 URL 中指定 XPath 查詢:
http://IISServer/nwind/schema/SampleSchema1.xml/child::Customer?root=root
虛擬名稱 schema 是 schema 類型。架構檔案儲存體在與 schema 類型虛擬名稱相關聯的目錄下。root 參數用於為所得到的 XML 文檔指定頂層元素(root 可為任意值)。
下面是模板執行的部分結果集:
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<Employee EmployeeID="1" LastName="Davolio"
FirstName="Nancy" Title="Sales Representative" />
<Employee EmployeeID="2" LastName="Fuller"
FirstName="Andrew" Title="Vice President, Sales" />
...
</ROOT>
B. 檢索上下文節點的孫節點
此 XPath 查詢選定上下文節點的 <Customer> 子項目的所有 <Order> 子項目:
/child::Customer/child::Order
在此查詢中,child 是軸,Customer 和 Order 是節點測試(如果 Customer 和 Order 是 <element> 節點,則這些節點測試為 TRUE,因為 <element> 節點是 child 軸的主要節點)。對於每個匹配 <Customer> 的節點,將匹配 <Orders> 的節點添加到結果中。結果集中只返回 <Order>。
child 是預設軸。因此,可將此查詢指定為:
/Customer/Order
在映射架構上測試 XPath 查詢
建立下面的模板 (MyTemplate.xml) 並將其儲存在與 template 虛擬名稱相關聯的目錄中。
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:xpath-query mapping-schema="SampleSchema1.xml">
/Customer/Order
</sql:xpath-query>
</ROOT>