View comments | RSS feed
Contents > Developing ColdFusion MX Applications > Using XML and WDDX > Extracting data with XPath PreviousNext

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 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 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 www.w3.org/TR/xpath. Most books that cover XSLT also discuss XPath.


Contents > Developing ColdFusion MX Applications > Using XML and WDDX > Extracting data with XPath PreviousNext

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

Version 6.1

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

Comments


No screen name said on Aug 24, 2004 at 3:28 PM :
I'd like to XMLTransform the results of an XMLSearch. But I cannot get the array returned from the XMLSearch back into a 'transformable' string or XML doc without a great deal of extra coding. Why? Shouldn't I just be able to:
mystoryXML = xmlSearch(xmldoc, "/publication/story[id="&storyid&"]");
output = xmltransform(mystoryXML, storyxsl);
No screen name said on Nov 26, 2004 at 12:31 PM :
I am trying to do the same thing. Can someone who knows the answer to this question post the answer here?

thanks.
jrunrandy said on Nov 27, 2004 at 1:30 PM :
I'm sorry, but LiveDocs does not receive enough traffic to handle questions like this effectively. I recommend that you post your issue to the online forums: http://webforums.macromedia.com/coldfusion/
No screen name said on Jan 9, 2007 at 3:25 PM :
The docs claim that you can use XPath to access attributes, but ColdFusion dies on '@' in an XPath (using W3C node/child@attribute syntax). It appears that you have to access the attributes as elements in an XMLAttributes childnode, not via XPath. Is there a way to use the correct XPath syntax?

 

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

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/xml37.htm