1

extension of my question . I able to come out with thw xsd for soap xml with the help of kjhughes. Now i want pass the soap xml to my xsd using coldfusion test page, but i get this error

[Error] :2:170: cvc-elt.1: Cannot find the declaration of element 'soap:Envelope'.

I wondering why i able to validation the xsd in here, but cannot validate in my coldfusion test page? What should I do to eliminate the error?

below is my test page

<cfsavecontent variable="sXML"><?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Cancel_OrderLine xmlns="http://tempuri.org/">
      <Data>
        <Delivery>
          <Delivery_No>1605000194</Delivery_No>
          <Reason>qwertyu</Reason>
        </Delivery>
        <Delivery>
          <Delivery_No>1605000194</Delivery_No>
          <Reason>qwerty</Reason>
        </Delivery>
      </Data>
    </Cancel_OrderLine>
  </soap:Body>
</soap:Envelope>
</cfsavecontent>

<cfset myResults = XMLValidate(sXML, "http://127.0.0.1:8501/test/schema/test_to_validate.xsd")>

<cfoutput>
  Did the sample xml validate against test_to_validate.xsd? #myResults.status#<br><br>
</cfoutput>

Dump of myResults structure returned by XMLValidate<br>
<cfdump var="#myResults#">
Community
  • 1
  • 1
chyman-91
  • 263
  • 3
  • 5
  • 12

1 Answers1

0

I think it might be because xmlValidate() in ColdFusion does not support namespaces. I've had the same problem before and my colleague wrote a wonderful piece of code to make an xmlValidate function to include namespaces. It seems that the underlying Java has an option to include namespaces in the validation, but ColdFusion automatically sets this option to 'false'. So the function my colleague wrote gets into the Java and does its validation there.

Here it is:

    <cffunction name="cfValidateXML" access="public" output="false" returntype="struct">
    <cfargument name="xmlPath" required="true">
    <cfargument name="xsdPath" required="true">
    <cfset var returnStruct = structNew()>
    <cfscript>
        try {
            /*Set up XML parser*/
            documentBuilderFactoryInstance = createobject("java","javax.xml.parsers.DocumentBuilderFactory").newInstance();
            /* Specify that the parser produced by this code provide support for XML namespaces. By default this value is false */
            documentBuilderFactoryInstance.setNamespaceAware(true);
            parser = documentBuilderFactoryInstance.newDocumentBuilder();
            XMLfile = fileRead(xmlPath);
            XSDfile = fileRead(xsdPath);
            xmlStringRead = createobject("java","java.io.StringReader").init(XMLfile);
            inputXMLSource = createobject("java","org.xml.sax.InputSource").init(xmlStringRead);
            xsdStringRead = createobject("java","java.io.StringReader").init(XSDfile);
            inputXSDSource = createobject("java","org.xml.sax.InputSource").init(xsdStringRead);
            /* Parse an XML document into a DOM tree */
            document = parser.parse(inputXMLSource);
            /* Create a SchemaFactory capable of understanding WXS schemas */
            factory = createobject("java","javax.xml.validation.SchemaFactory").newInstance(createobject("java","javax.xml.XMLConstants").W3C_XML_SCHEMA_NS_URI);
            /* Load a WXS schema, represented by a Schema instance */
            schemaFile = createobject("java","javax.xml.transform.stream.StreamSource").init(xsdStringRead);
            schema = factory.newSchema(schemaFile);
            /* Create a Validator instance, and use it to validate an instance document */
            validator = schema.newValidator();
            domResult = validator.validate(createobject("java","javax.xml.transform.dom.DOMSource").init(document), createobject("java","javax.xml.transform.dom.DOMResult"));
            returnStruct = {
                status = true
            };
            return returnStruct;
        }
        catch (any result) {
            returnStruct = {
                status = false,
                errors = result
            };
            return returnStruct;
        }
    </cfscript>
</cffunction>

Note that this uses a path to the xml-file and xsd-path, but you could easilly change that to strings.

I created a piece of code on tryCF where the function is included in your testcode: here

Sander
  • 390
  • 1
  • 4
  • 13
  • Thank you for your code, can i change the xsd-path to a file that all schema had include in? eg. http://127.0.0.1:8501/test/schema/test_to_validate.xsd – chyman-91 Oct 07 '16 at 01:10
  • i had test with the code, i found out that even i put in non-integer value into the Delivery_No or exchange the sequence of Reason & Delivery_No, it also can be validate. Why it didn't return false for the result? – chyman-91 Oct 07 '16 at 02:35
  • i had try it but i cant work and if i change the int of the delivery_no to no integer or sequence of the Reason & Delivery_No, i cannot do any validation for me as well – chyman-91 Oct 07 '16 at 07:42
  • Yeah, I noticed this. I think there is something wrong with the XSD. When I test this with one of our own xsd's and xml's, the type int validates only if the content of the element is an int. Unfortunately, I don't have that much experience with building xsd's – Sander Oct 07 '16 at 08:35
  • oic, is there any others solution? – chyman-91 Oct 07 '16 at 08:36
  • You could try to specify the format of your elements more by using restrictions. Like I said, I don't know much about building an xsd, but there's a good tutorial for that on [w3schools](http://www.w3schools.com/xml/schema_facets.asp) – Sander Oct 07 '16 at 10:19