XPath or XML Path Language is a W3C Specification that is designed to be used to address parts of an XML document. XPath expressions provide an uniform and compact syntax that can be used to look for nodes that match some criteria, instead of writing code to explicitly walk through a document's structure. XPath is generally used in conjunction with XSLT. MSXML supports XPath 1.0.
Tip: Chapter 4 [XPath in MSXML] in the book XML Application Development with MSXML 4.0 covers MSXML XPath implementation details in great details.
- MSXML 3.0 XPath Documentation
- MSXML 3.0 XPath Reference
- MSXML 4.0 XPath Documentation
- MSXML 4.0 XPath Reference
Don't forget to check out 50 XSLT Tips and XPath/XSLT Quiz.
Interactive XPath expression builder by Aaron Skonnard:
(MSXML 3.0 version MSXML 4.0 version MSXML Version Independent)
Example: Try XPath Expression: /Invoices/Invoice/LineItems/LineItem[Sku='134']/Price/node()
Tip: Check out an excellent article on XPath by Aaron Skonnard.
Let's look at JavaScript example to use XPath expressions with MSXML:
Conside following sample XML document (c:\soapres.xml):
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddResponse>
<sum>100</sum>
</AddResponse>
</soap:Body>
</soap:Envelope>
JavaScript
<script language="JScript">
<!--
try
{
var objDOMDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
objDOMDoc.async = false;
if (objDOMDoc.load("c:\\soapres.xml"))
{
objDOMDoc.setProperty("SelectionNamespaces", "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'");
alert(objDOMDoc.selectSingleNode("/soap:Envelope/soap:Body/AddResponse/sum").nodeTypedValue);
}
else
{
alert("Error: " + objDOMDoc.parseError.reason);
}
}
catch(e)
{
alert("Error: " + e);
}
//-->
</script>
MSXML XPath Resources Around the Web
- Object Graphs, XPath, String Comparisons, and More
- MSXML XPath KB Articles
- Addressing Infosets with XPath
- XPath in MSXML
|