Skip to main content
ilssac
Inspiring
July 12, 2010
Answered

Validating an XML document to a schema using ColdFusion

  • July 12, 2010
  • 1 reply
  • 3660 views

This is something I have never tried before.  We created an XML Schema  to define XML documents we expect to receive from various entities.   When we receive the document, we would like to validate it before  processing it.  I think ColdFusion is up to this from reading the  documentation, but we have not got anything working yet.

When we try and xmlParse() our test XML file against the XML schema we  get the following error.  When we use a web based XML validation tool  and feed it the same XML file and schema it validates just fine.

An error occured while parsing an XML document.
[Error] :2:6: cvc-elt.1: Cannot find the declaration of element 'pur'.
  
The error occurred in D:\playground\warren\ppur_file_import.cfm: line 57
55 :
56 :
57 : <cfset xmldoc = XmlParse(ExpandPath(filepath), true,  ExpandPath(validator)) />
58 : <cfdump var="#xmldoc#">
59 : <cfabort>

Searching for the error has not provided me any useful hints.  Can  anybody here?

This topic has been closed for replies.
Correct answer JR__Bob__Dobbs

Smaller sample file, producing the same error.

XML Schema

<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:element name="pur">    
          <xs:complexType>         
               <xs:sequence>              
         
                    <xs:element name="File" type="xs:string" minOccurs="1" maxOccurs="1"/>

                    <xs:element name="Data" type="xs:string" minOccurs="1" maxOccurs="1"/>
                   
               </xs:sequence>
          </xs:complexType>
     </xs:element>
</xs:schema>

XML Test FIle

<?xml version="1.0" encoding="iso-8859-1"?>
<pur>
     <File>foo</File>
     <Data>bar</Data>
</pur>

These files, and the large files I provided previously will validate correctly according to this tool.

http://tools.decisionsoft.com/schemaValidate/


I was able to get the sample document to parse by adding namespaces to the sample file.  I'm not an XML expert so I can't provide a clear explanation for this.  I used Altova XML Spy to create an XML sample document based on your schema and it added the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation sections to the XML file.  Note that the value of noNamespaceSchemaLocation would normally be the path to the XSD file.  I have left the value blank and the sample still parses in ColdFusion 8.

I suspect the underlying issue is related to how CF's Java based XML parser expects namespaces to be handled in XML documents, but that is just a theory.

<cfsavecontent variable="sample"><?xml version="1.0" encoding="iso-8859-1"?><pur xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="">
     <File>foo</File>
     <Data>bar</Data>
</pur>
</cfsavecontent>

<cfsavecontent variable="theSchema"><?xml version="1.0" encoding="iso-8859-1"?>  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:element name="pur">
          <xs:complexType>
               <xs:sequence>              
         
                     <xs:element name="File" type="xs:string" minOccurs="1" maxOccurs="1"/>

                    <xs:element name="Data" type="xs:string" minOccurs="1" maxOccurs="1"/>
                   
               </xs:sequence>
          </xs:complexType>
     </xs:element>
</xs:schema>
</cfsavecontent>


<cfset xmldoc1=XmlParse(sample, true, theSchema) />
<cfdump var="#xmldoc1#" />

1 reply

Inspiring
July 12, 2010

Can you post the XML file and XML schema?

ilssac
ilssacAuthor
Inspiring
July 12, 2010

I can, but they are a bit large.

Inspiring
July 12, 2010

Can you post smaller files that reproduce the same error?