View comments | RSS feed

XmlSearch

Description

Uses an XPath language expression to search an XML document that is represented as a string variable.

Return value

An array of XML object nodes that match the search criteria.

Category

Extensibility functions, XML functions

Syntax

XmlSearch(xmlDoc, xPathString)

See also

cfxml, IsXmlDoc, XmlChildPos, XmlChildPos, XmlFormat, XmlNew, XmlParse, XmlTransform

History

New in ColdFusion MX: this function is new.

Parameters

Parameter Description
xmlDoc
XML document object
xPathString
XPath expression

Usage

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.

Example

The following example extracts the elements named last, which contain employee 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>

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


dave-007 said on Jun 19, 2003 at 8:16 PM :
Can we see more xpath query examples? It's confusing for me still.
No screen name said on Aug 11, 2003 at 2:09 AM :
It's worth saying that XmlSearch() works only with XPath expressions that returns a “node list” and, unfortunately, it throw an error if the XPath expression returns a string, a boolean or a number. For example, the code below should return the number of elements available in a XML document, but it throws an error. XmlSearch(xmlDoc,"count(//*)") While the same functionality can be easily achieved accessing the XML doc as a structure, this issue prevents us from taking advantage of many useful XPath functionalities.
mvecera said on Sep 12, 2003 at 11:24 PM :
This page ABSOLUTELY needs a list of unsupported XPath syntax and functions. The trial-and-error to find out what portion of XPath you can use is maddening.
WebPexDev said on May 21, 2007 at 4:43 PM :
As far as I can tell there is no way to specify xmlns namespaces for an xpath search. If you have an xml document with tags in two different namespaces you cannot search for nodes in the namespace that appears second. Normally you would add the additional namespace as a property in your xsl template or with the xmlDdocument.setProperty("SelectionNamespaces",
"xmlns:some_ns")

 

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

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt2123.htm