692
Chapter 31: Using XML and WDDX
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.
Example: using XML in a ColdFusion application
The example in this section shows how you can use XML to represent data, and how ColdFusion
can use XML data in an application. Although the example is too simple to be used in an
application without substantial changes, it presents some of the common uses of XML with
ColdFusion.
The example receives an order in the form of an XML document, processes it, and generates an
XML receipt document. In this case, the order document is in a file, but it could be received as
the result of an HTTP request, or retrieved using
cfpop
,
cfftp
, or other methods. The
ColdFusion page does the following with the order:
1
Generates a query object from an XML document.
2
Queries a database table to determine the order discount percentage to use.
3
Uses a query of queries to calculate the total price, then calculates the discounted price.
4
Generates the receipt as an XML document.
This example displays the results of the processing steps to show you what has been done.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......