Adobe COLDFUSION 9 Manual page 1085

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Using Web Elements and External Objects
2
Uses the style sheet to transform the mydoc XML document object.
Saves the resulting transformed document in a second file.
3
<cffile action="read" file="C:\CFusion\wwwroot\testdocs\simpletransform.xsl"
variable="xslDoc">
<cfset transformedXML = XmlTransform(mydoc, xslDoc)>
<cffile action="write" file="C:\CFusion\wwwroot\testdocs\transformeddoc.xml"
output=transformedXML>
XSL and XSLT are specified by the World Wide Web Consortium (W3C). For detailed information on XSL, XSLT, and
XSL style sheets, see the W3C website at www.w3.org/Style/XSL/. Several books are available on using XSL and XSLT.
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
function uses XPath expressions to extract data from XML document objects. The function takes an
XmlSearch
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:
<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 following 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
Generates a query object from an XML document.
1
Queries a database table to determine the order discount percentage to use.
2
3
Uses a query of queries to calculate the total price, then calculates the discounted price.
, or other methods. The ColdFusion page does the following with the order:
Last updated 8/5/2010
1080

Advertisement

Table of Contents
loading

Table of Contents