View comments | RSS feed

Extracting data with XPath

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 the XmlPath 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 an array of XML document objects containing the elements that meet the expression criteria.

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:

<cffile action="read"
  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 http://www.w3.org/TR/xpath. Most books that cover XSLT also discuss XPath.

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


srhcfmx said on Jun 10, 2002 at 11:42 AM :
TYPO: The third paragraph beginning "The the XmlPath function" should be "The XmlSearch function"

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/Developing_ColdFusion_MX_Applications_with_CFML/XML9.htm