Validate XML in Classic ASP

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a project that consists of the following files:<br/> <br/> sample_data.xml<br/> feeData.xsd<br/> <br/> fees_language.xml<br/> fees_language.xsd<br/> <br/> fees.xslt<br/> <br/> The sample_data.xml references the feeData.xsd and is valid, fees_language references fees_language.xsd and is also valid.<br/> <br/> The XSLT itself is able to transform the XML in sample_data.xml to HTML, and uses the fees_language.xml as a variable in this way:<br/> <br/> <xsl:variable name="Language_Id" select="Fee_Root/Fees/@Language_Id"/><br/> <xsl:variable name="Language" select="document(fees_language.xml)/Languages/Language[@Language_Id=$Language_Id]"/><br/> <br/> This selects the correct language from my fees_language, and puts it into a variable.<br/> <br/> When I open my sample_data.xml in Firefox or Internet Explorer, it renders correctly. And when I debug my XSLT in Stylus Studio, it also renders correctly.<br/> <br/> However, I would like to check if the XML is valid when parsing in my ASP solution and here is how i do it:<br/> <br/> (Code simplified, I have removed a lot of error checking)<br/>
<pre lang=x-vbnet>-- Setup Objects
Set objXMLDOM = Server.CreateObject("MSXML2.DOMDocument.4.0")
Set objFreeDOM = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.4.0")
Set objXSLTemplate = Server.CreateObject("MSXML2.XSLTemplate.4.0")
Set objXSDCache = Server.CreateObject("Msxml2.XMLSchemaCache.4.0")

-- Add XSD to Cache, and then to the objXMLDOM
Dim XSDName : XSDName = Request.servervariables("APPL_PHYSICAL_PATH").item & "feeData.xsd"
objXSDCache.Add "", XSDName
set objXMLDOM.schemas = objXSDCache

-- Load XML, xml_string is XML generated from the database
objXMLDom.loadXML(xml_string)

-- Load XSL into a free threaded DOM
objFreeDOM.async = False
objFreeDOM.resolveExternals = False (I have also tested with True)
objFreeDOM.validateOnParse = False
objFreeDOM.load(filename)

-- Attach XSL to Template
objXSLTemplate.stylesheet = objFreeDOM

-- Create the processor
Dim processor
set processor = objXSLTemplate.createProcessor

-- Transform
processor.transform

-- And then output
result = processor.output[/code]
<br/> When i run this code i get an error:<br/> <br/> Error while parsing "file:///F:/CVS/fees_language.xml". The element Languages is used but not declared in the DTD/Schema. <br/> <br/> It says that the Languages element is not part of the feeData.xsd - Thats correct, its part of its own XSD named fees_language.xsd.<br/> <br/> Is there a way of getting around this error?<br/> <br/> I would prefer to have separate XSDs for each of my XML files, but I also would like to know if the XML fed to the XSL is bad. And putting it all into one XSD is also something Id like to avoid.<br/> <br/> If I omit the line:<br/>
<pre lang=x-vbnet>set objXMLDOM.schemas = objXSDCache[/code]
It works like a charm (but is not validating the XML)<br/> <br/> Files can be zipped and made available upon request.<br/> <br/> Best regards,<br/> Michael Nielsen<br/>

View the full article
 
Back
Top