XML e ColdFusion : XmlTransform()
COLDFUSION E XML
XmlTransform()
La funzione XmlTransform() viene usata pertrasformare un documento XML attraverso fogli di stile XSLT.Questa funzione ritorna una stringa contenente il risultatodi tale trasformazione. La funzione ha i seguenti parametri,mostrati nella tabella qui sotto:
Parametro | Descrizione |
|---|---|
xml | Obbligatorio. Può assumere uno dei seguentivalori:
|
xsl | Obbligatorio. Può assumere uno dei seguentivalori:
|
parameters | Opzionale. Rappresenta una struttura conenete le coppienome-valore che rimandano ai parametridefiniti nel documento XSLT. Le proprietà dellastruttura devono essere aggiunte tramite la funzioneStructInsert(). |
Per illustrarne le funzionalità, useremo i seguentidocumenti XML e XSLT:
Es.4: George.xml
<?xml version="1.0" encoding="iso-8859-1"?> <Name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Name.xsd" Title="President"> <FirstName>George</FirstName> <LastName>Washington</LastName> </Name>
Es.5: Name.xsl
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="bgcolor" select="'white'"/> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <html><head> <title> <xsl:value-of select="Name/@Title"/> <xsl:text> </xsl:text> <xsl:value-of select="Name/FirstName"/> <xsl:text> </xsl:text> <xsl:value-of select="Name/LastName"/> </title></head><body bgcolor="{$bgcolor}"> <h1> <xsl:value-of select="Name/@Title"/> <xsl:text> </xsl:text> <xsl:value-of select="Name/FirstName"/> <xsl:text> </xsl:text> <xsl:value-of select="Name/LastName"/></h1></body> </html> </xsl:template> </xsl:stylesheet>
Si noti che Name.xsl accetta un parametrobgcolor, che viene usato per settare il coloredello sfondo della pagina. Il seguente file ColdFusiontrasformerà George.xml attraverso ildocumento Name.xsl.
Es.6: XmlTransform.cfm
<cffile action="read" variable="strXML" file="#ExpandPath('George.xml')#"> <cfset PathToXslt = ExpandPath("Name.xsl")> <cfset params = StructNew()> <cfset StructInsert(params,"bgcolor" ,"silver")> <cfoutput>#XmlTransform(strXML,PathToXslt,params)#</cfoutput>
Si noti che dobbiamo leggere il file XML e memorizzarla inuna stringa attraverso <cffile> prima dipassarla a XmlTransform(), che non puòaccettare come argomento il percorso ad un file XML. Ilcodice HTML risultante sarà il seguente:
<?xml version="1.0" encoding="UTF-8" ?><html><head><title>President George Washington</title></head><body bgcolor="silver"><h1>President George Washington</h1></body></html>
La pagina apparirà così nel vostro browser:
Fig 2: risultato dell'esempio 6
XmlFormat()
La funzione XmlFormat() viene usata pereffettuare l'escaping dei caratteri speciali in modo taleda poterli visualizzare correttamente nel browser. Peresempio il carattere < diventa<. L'esempio seguente mostra comepotremmo rappresentare il codice risultante dallatrasformazione che avevamo fatto nella finestra del browser.
Es. 7: XmlFormat.cfm
<cffile action="read" variable="strXML" file="#ExpandPath('George.xml')#"> <cfset PathToXslt = ExpandPath("Name.xsl")> <cfset params = StructNew()> <cfset StructInsert(params,"bgcolor" ,"silver")> <cfset result = XmlTransform(strXML,PathToXslt,params)> <cfset formattedXml = XmlFormat(result)><html> <head> <title>XmlFormat()</title> </head> <body> <h1>XmlFormat()</h1> <cfoutput> <pre>#formattedXml#</pre> </cfoutput> </body> </html>
La pagina risultante è la seguente:
Fig 3: risultato dell'esempio 7
