XPath is a language for addressing parts of an XML document. Like XSL, XPath is a W3C specification. One of the major uses of XPath is in XSL transformations. However, XPath has more general uses. In particular, it can extract data from XML documents, such as complex data set representations. Thus, XPath is another data querying tool.
XPath uses a pattern called an XPath expression to specify the information to extract from an XML document. For example, the simple XPath expression /employee/name selects the name elements in the employee root element.
The XmlSearch function uses XPath expressions to extract data from XML document objects. The function takes an XML document object and an XPath expression in string format, and returns the results of matching the XPath expression with the XML. The returned results can be any XPath return type that ColdFusion can represent, such as an array of XML object nodes or a Boolean value. For more information, see XmlSearch in the CFML Reference.
The following example extracts all the elements named last, which contain the employee's last names, from the employeesimple.xml file, and displays the names:

file="C:\inetpub\wwwroot\examples\employeesimple.xml"
variable="myxml">
<cfscript>
myxmldoc = XmlParse(myxml);
selectedElements = XmlSearch(myxmldoc, "/employee/name/last");
for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
writeoutput(selectedElements[i].XmlText & "<br>");
</cfscript>

XPath is specified by the World Wide Web Consortium. For detailed information on XPath, see the W3C website at www.w3.org/TR/xpath. Most books that cover XSLT also discuss XPath.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy