Monday, June 8, 2009

Transforming xml String in ASP/vbScript

When using the Microsoft XMLDOM in ASP there are plenty of tutorials like the ones at w3schools.com that describe loading and transforming an xml file on the server. In this case we were calling to a web service that returned xml in a string and wanted to send that string through an xslt transformation.

If you use the xmlDom.load(variableName) function like many of the examples you receive a error like:

The filename, directory name, or volume label syntax is incorrect


To correct this you need to use the xmlDom.loadXml(variableName)

Here is a sample ASP vbscript to show it working, notice the loadXml on line 29.



<%@ Language=VBScript %>

<%Option Explicit%>

<%
Dim xslt
Dim xslProc
Dim xslDoc
Dim xmlDoc
Dim sXslt
Dim sResponse
Dim sXmlString
Dim sErrMsg

sXslt = "myXslt.xsl"
sXmlString = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" & vbCrLf
sXmlString = sXmlString & "<catalog>"& vbCrLf
sXmlString = sXmlString & "<cd><title>The Alter and The Door</title><artist>Casting Crowns</artist></cd>" & vbCrLf
sXmlString = sXmlString & "<cd><title>The Day Of</title><artist>David Wells</artist></cd></catalog>"

set xslDoc = Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM")
xslDoc.async = false
xslDoc.load(Server.MapPath(sXslt))

if xslDoc.parseError.errorCode = 0 Then
set xmlDoc = Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM")
xmlDoc.async = false
xmlDoc.loadXml(sXmlString)
if xmlDoc.parseError.errorCode = 0 Then
set xslt = Server.CreateObject("Msxml2.XSLTemplate")
xslt.stylesheet = xslDoc
set xslProc = xslt.createProcessor()
xslProc.addParameter "userid" , "1"
xslProc.addParameter "amount" , "100"
xslProc.addParameter "paymentDescriptor" , "Sample"
xslProc.addParameter "feeDescriptor" , "fee Sample"
xslProc.input = xmlDoc
xslProc.transform()
sResponse = xslProc.output
else
response.write "Internal Error " & xml2Doc.parseError.reason
end if
else
response.write "Internal Error : " & xslDoc.parseError.reason
end if

response.write "xslt = " & Server.HTMLEncode(xslDoc.xml) & "<hr>"
response.write "input xml String = " & Server.HTMLEncode(sXmlString) & "<hr>"
response.write "output xml = " & Server.HTMLEncode(sResponse)

%>


The xslt in this simplified example is:


<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="userid"/>
<xsl:param name="amount" />
<xsl:param name="paymentDescriptor"/>
<xsl:param name="feeDescriptor"/>
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes" />
<xsl:template match="/">
<transaction>
<userid><xsl:value-of select="$userid"/></userid>
<amount><xsl:value-of select="$amount"/></amount>
<payment_descriptor><xsl:value-of select="$paymentDescriptor"/></payment_descriptor>
<catalog>
<xsl:for-each select="catalog/cd">
<cd>
<title><xsl:value-of select="title"/></title>
<artist><xsl:value-of select="artist"/></artist>
<fee_descriptor><xsl:value-of select="$feeDescriptor"/></fee_descriptor>
</cd>
</xsl:for-each>
</catalog>
<status_code><xsl:value-of select="transaction/status_code"/></status_code>
<status_text><xsl:value-of select="transaction/status_text"/></status_text>
</transaction>
</xsl:template>
</xsl:stylesheet>

No comments:

Post a Comment