Advanced XML


In addition to the basic reading and writing of XML data, ColdFusion also supports XSL styles, XPath searches, and low-level access to XML data when needed.

Applying XSL Stylesheets

XSL (Extensible Stylesheet Language) is used to define formatting and output instructions to be applied to XML. ColdFusion itself does not create or manage XSL style sheets, but it does provide a function that may be used to apply style sheetsreturning the transformed data.

The process of applying style sheets is known as XSLT (Extensible Stylesheet Language Transformation) and is performed using the XMLTransform() function, like this:

 <cffile action="read"         file="C:\cfusionmx7\wwwroot\xsl\standard.xsl"         variable="xslDoc"> <cfoutput> #XMLTransform(users, xslDoc)# </cfoutput> 

Here an XSL style sheet is read using <cffile> and then used in an XMLTransform().

NOTE

XMLTransform() can take an XML Document Object and an XML document (in string form) as its first argument.


Performing Searches

Extracting data from an XML document is not difficult, but searching for data can be. As XML documents are made up of both data and tags, it can be tricky to search for specific data or tags or at specific nesting levels. XPath is a searching language not unlike what Regular Expressions are for textan XPath search involves creating a search pattern and then using it to extract data from an XML document.

XPath is supported in ColdFusion by the XMLSearch() function, which takes a search pattern and an XML Document Object to be searched. A simple example is seen here:

 <cfset results=XMLSearch(users, "user/UserID")> <cfoutput> Found #ArrayLen(results)# matches </cfoutput> 

This example searches for user elements containing a UserID element and returns an array of matches.

NOTE

XPath is defined by the W3; details are at http://www.w3.org/TR/xpath.


Other XML Functions

In addition to the XML functions used thus far, ColdFusion provides several other XML functions that may be used as needed:

  • IsXML() is used to test whether or not a string is well formed XML.

  • IsXMLAttribute() is used to test whether or not a parameter is a valid node attribute.

  • IsXMLDoc() is used to test whether a variable contains a valid XML Document Object.

  • IsXMLElem() is used to test whether or not a parameter is a valid object element.

  • IsXMLNode() is used to test whether or not a parameter is a valid object node.

  • IsXMLNode() is used to test whether or not a parameter is the root of an XML object.

  • XMLChildPos() is used to locate specific child elements.

  • XMLElemNew() is used to add an element to an XML Document Objects.

  • XMLGetNodeType() is used to determine the type of an XML document object.

  • XMLFormat() is used to escape text so as to make it safe for inserting within an XML document.



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net