Transforming documents with XSLT
691
Converting a query object to XML
The following example shows how to convert a query object to XML. It uses
cfquery
to get a list
of employees from the CompanyInfo database and saves the information as an XML document.
<!--- Query the database and get the names in the employee table --->
<cfquery name="myQuery" datasource="CompanyInfo">
SELECT FirstName, LastName
FROM employee
</cfquery>
<!--- Create an XML document object containing the data --->
<cfxml variable="mydoc">
<employee>
<cfoutput query="myQuery">
<name>
<first>#FirstName#</first>
<last>#LastName#</last>
</name>
</cfoutput>
</employee>
</cfxml>
<!--- dump the resulting XML document object --->
<cfdump var=#mydoc#>
<!--- Write the XML to a file --->
<cffile action="write" file="C:\inetpub\wwwroot\xml\employee.xml"
output=#toString(mydoc)#>
Transforming documents with XSLT
The Extensible Stylesheet Language Transformation (XSLT) technology transforms an XML
document into another format or representation. For example, one common use of XSLT is to
convert XML documents into HTML for display in a browser. XSLT has many other uses,
including converting XML data to another format, such as converting XML in a vocabulary used
by an order entry application into a vocabulary used by an order fulfillment application.
XSLT transforms an XML document by applying an Extensible Stylesheet Language (XSL)
stylesheet. (When stored in a file, XSL stylesheets typically have the suffix xsl.) ColdFusion
provides the
XmlTransform
function to apply an XSL transformation to an XML document.
The function takes an XML document in string format or as an XML document object, and an
XSL stylesheet in string format, and returns the transformed document as a string.
The following code:
1
Reads the simpletransform.xsl stylesheet file into a string variable.
2
Uses the stylesheet to transform the mydoc XML document object.
3
Saves the resulting transformed document in a second file.
<cffile action="read" file="C:\Neo\wwwroot\testdocs\simpletransform.xsl"
variable="xslDoc">
<cfset transformedXML = XmlTransform(mydoc, xslDoc)>
<cffile action="write" file="C:\Neo\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 stylesheets, see the W3C website at
www.w3.org/Style/XSL/. There are also several books on using XSL and XSLT.
Summary of Contents for 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: ......