Home >Backend Development >XML/RSS Tutorial >Specifying axis in XPath query (reprinted from MSSQL manual)
The following example shows how to specify an axis in an XPath query. The XPath queries in these examples are specified on the mapping schema contained in SampleSchema1.xml. For information about this sample schema, see Sample XPath Query.
Example
A. Retrieve the child elements of the context node
This XPath queries all c214b1127c801bd6a2a45c5b466f54b2 child elements of the selected context node:
/child::Employee
In this query, child is the axis and Customer is the node test (this test is TRUE if Customer is an 1dd643d91c792abd0a34d1b312e2f207 node because 1dd643d91c792abd0a34d1b312e2f207 is the primary node type associated with the child axis).
child is the default axis. Therefore, the query can be written as:
/Employee
Testing the XPath query on the mapping schema
Create the following template (MyTemplate.xml) and save it in the directory associated with the template virtual name .
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <sql:xpath-query mapping-schema="SampleSchema2.xml"> /Employee </sql:xpath-query> </ROOT>
The following URL executes the template:
http://IISServer/VirtualRoot/template/MyTemplate.xml
XPath query can be specified directly in the URL:
http://IISServer/nwind/schema/SampleSchema1.xml/child::Customer?root=root
The virtual name schema is the schema type. Schema files are stored in the directory associated with the schema type virtual name. The root parameter specifies the top-level element for the resulting XML document (root can be any value).
The following is a partial result set of template execution:
<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. Retrieve the grandchild nodes of the context node
This XPath queries all < of the c214b1127c801bd6a2a45c5b466f54b2 child elements of the selected context node ;Order> Child elements:
/child::Customer/child::Order
In this query, child is the axis, Customer and Order are node tests (if Customer and Order are 1dd643d91c792abd0a34d1b312e2f207 nodes, then These nodes test TRUE because the 1dd643d91c792abd0a34d1b312e2f207 node is the primary node of the child axis). For each node that matches c214b1127c801bd6a2a45c5b466f54b2, a node that matches 100fdf8091fa655ad847e46c0cb82106 is added to the result. Only 7e7d629a61e1c649d53a7e569305f73a is returned in the result set.
child is the default axis. Therefore, this query can be specified as:
/Customer/Order
Testing the XPath query on the mapping schema
Create the following template (MyTemplate.xml) and save it in the in the directory.
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"> <sql:xpath-query mapping-schema="SampleSchema1.xml"> /Customer/Order </sql:xpath-query> </ROOT>
The above is the content of specifying the axis in the XPath query (reprinted from the MSSQL manual). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!