Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Problem Validating XML Document

From: MartinL@-----------.---------.---
To: NULL
Date: 1/13/2009 3:12:00 PM
Hello,

I am creating an XML document in MSXML5 and attempting to validate it after 
I've completed it.  Validating the document fails with an error code of 
-1072897500 and the message "The node is neither valid nor invalid because no 
DTD/Schema declaration was found."  Oddly, if I save the XML document and 
read it back in again, it validates.  Some sample code that produces this 
behavior is below, along with the xsd file.  Can someone help resolve this 
issue?

Regards,
Martin

Code:

    ' Initialize the XML instance document and the schema cache.  Add the 
schema to the
    ' cache and assign it to the instance document.
    
    Dim xmlDocument As MSXML2.DOMDocument50
    Set xmlDocument = New MSXML2.DOMDocument50
    xmlDocument.async = False
    xmlDocument.validateOnParse = True
    xmlDocument.resolveExternals = True
    xmlDocument.preserveWhiteSpace = True
    
    Dim xmlSchemaCache As MSXML2.XMLSchemaCache50
    Set xmlSchemaCache = New MSXML2.XMLSchemaCache50
    xmlSchemaCache.Add "http://foo.com", "C:\Userdata\book.xsd"
    Set xmlDocument.Schemas = xmlSchemaCache
    
    
    ' Add the processing instruction.

    Dim xmlProc As IXMLDOMProcessingInstruction
    Set xmlProc = xmlDocument.createProcessingInstruction("xml", 
"version='1.0' encoding='UTF-8'")
    xmlDocument.appendChild xmlProc
    Set xmlProc = Nothing


    ' Create the collection root element with the namespace and schema 
attributes, and add it to the document.
    
    Dim elmCollection As MSXML2.IXMLDOMElement
    Set elmCollection = xmlDocument.createNode(NODE_ELEMENT, "b:Collection", 
"http://foo.com")
    
    Dim atrNSNode As IXMLDOMAttribute
    Set atrNSNode = xmlDocument.createNode(NODE_ATTRIBUTE, "xmlns:xsi", 
"http://www.w3.org/2001/XMLSchema-instance")
    atrNSNode.Value = "http://www.w3.org/2001/XMLSchema-instance"
    elmCollection.setAttributeNode atrNSNode
    Set atrNSNode = xmlDocument.createNode(NODE_ATTRIBUTE, 
"xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance")
    atrNSNode.Value = "http://foo.com C:\Userdata\book.xsd"
    elmCollection.setAttributeNode atrNSNode
    Set atrNSNode = Nothing
    
    xmlDocument.appendChild elmCollection
    
    
    ' Create the Book element and its children, and add it to the document.
    
    Dim elmBook As MSXML2.IXMLDOMElement
    Set elmBook = xmlDocument.createElement("b:Book")
    elmCollection.appendChild elmBook
    
    Dim elmTitle As IXMLDOMElement
    Dim elmAuthor As IXMLDOMElement
    Dim elmPublisher As IXMLDOMElement
    Set elmTitle = xmlDocument.createElement("b:Title")
    Set elmAuthor = xmlDocument.createElement("b:Author")
    Set elmPublisher = xmlDocument.createElement("b:Publisher")

    elmTitle.Text = "Lover Birds"
    elmAuthor.Text = "Cynthia Randall"
    elmPublisher.Text = "Lucerne Publishing"
    
    elmBook.appendChild elmTitle
    elmBook.appendChild elmAuthor
    elmBook.appendChild elmPublisher
    
    Set elmTitle = Nothing
    Set elmAuthor = Nothing
    Set elmPublisher = Nothing
    Set elmBook = Nothing


    ' Validate the document, write it to a file, then read it back in and 
validate again.
    
    Dim xmlParseError As MSXML2.IXMLDOMParseError
    Set xmlParseError = xmlDocument.validate               ' <== Does not 
validate
    MsgBox "Error Code: " & xmlParseError.errorCode & vbNewLine & 
xmlParseError.reason
    Set xmlParseError = Nothing
    
    xmlDocument.Save "C:\Userdata\book.xml"
    
    Set xmlDocument = Nothing
    Set xmlDocument = New MSXML2.DOMDocument50
    xmlDocument.Load ("C:\Userdata\book.xml")
    Set xmlParseError = xmlDocument.validate               ' <== Validates 
successfully
    MsgBox "Error Code: " & xmlParseError.errorCode & vbNewLine & 
xmlParseError.reason
    Set xmlParseError = Nothing



XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://foo.com" elementFormDefault="qualified">
   <xsd:element name="Collection">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="Book">
            <xsd:complexType>
             <xsd:sequence>
                <xsd:element name="Title" type="xsd:string"/>
                <xsd:element name="Author" type="xsd:string"/>
                <xsd:element name="Publisher" type="xsd:string"/>
             </xsd:sequence>
           </xsd:complexType>
         </xsd:element>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>
</xsd:schema>


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent