Discussion:
Problem Validating XML Document
(too old to reply)
MartinL
2009-01-13 23:12:01 UTC
Permalink
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>
Martin Honnen
2009-01-14 11:51:53 UTC
Permalink
Post by MartinL
Dim atrNSNode As IXMLDOMAttribute
Set atrNSNode = xmlDocument.createNode(NODE_ATTRIBUTE, "xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance")
The namespace for namespace declaration attributes (i.e. xmlns:prefix)
is http://www.w3.org/2000/xmlns/ so the last argument above should be
"http://www.w3.org/2000/xmlns/"

Try whether that solves it, I haven't looked at the other code in detail.
Post by MartinL
Set elmBook = xmlDocument.createElement("b:Book")
If you want to create XML with namespaces with MSXML then I strongly
suggest to use createNode as that is the only namespace aware method
that MSXML provides. So change _all_ those createElement calls to e.g.
createNode(1, "b:book", "http://foo.com")
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
MartinL
2009-01-14 12:50:01 UTC
Permalink
Hi Martin,

Thanks for the quick reply. I made both changes as you suggested, but it
did not have any effect - the XML that was created was identical to the
original version of the code, and the error message still occurs. What is
even more bizarre is if I output the XML as a string right before I perform
the first validation, the XML is perfectly fine (the schema declaration is
there), but it still gives the message that the schema declaration was not
found.

Regards,
Martin
Post by Martin Honnen
Post by MartinL
Dim atrNSNode As IXMLDOMAttribute
Set atrNSNode = xmlDocument.createNode(NODE_ATTRIBUTE, "xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance")
The namespace for namespace declaration attributes (i.e. xmlns:prefix)
is http://www.w3.org/2000/xmlns/ so the last argument above should be
"http://www.w3.org/2000/xmlns/"
Try whether that solves it, I haven't looked at the other code in detail.
Post by MartinL
Set elmBook = xmlDocument.createElement("b:Book")
If you want to create XML with namespaces with MSXML then I strongly
suggest to use createNode as that is the only namespace aware method
that MSXML provides. So change _all_ those createElement calls to e.g.
createNode(1, "b:book", "http://foo.com")
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Martin Honnen
2009-01-14 13:01:00 UTC
Permalink
Post by MartinL
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?
I have now looked at the details and MSXML 5 and MSXML 6 behave
unfortunately quite different.
With MSXML 6 and the validate method I get the correct result with the
following samples:
Schema test2009011401Xsd.xml:

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/2009/ns1"
elementFormDefault="qualified">

<xs:element name="root">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="foo" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

VBScript using MSXML 6:

Const ns1 = "http://example.com/2009/ns1"
Dim doc : Set doc = CreateObject("Msxml2.DOMDocument.6.0")
Dim root : Set root = doc.createNode(1, "root", ns1)
Dim foo : Set foo = doc.createNode(1, "foo", ns1)
foo.text = "1"
root.appendChild foo
doc.appendChild root

Dim schemas : Set schemas = CreateObject("Msxml2.XMLSchemaCache.6.0")
schemas.add ns1, "test2009011401Xsd.xml"
Set doc.schemas = schemas

Dim error : Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason

Set foo = doc.createNode(1, "foo", ns1)
foo.text = "a"
root.appendChild foo
Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason

outputs (error messages are in German as I am on a German Windows XP)

0:
-1072897535: Fehler beim Analysieren von 'a' als int-Datentyp.
Analyse des Elements '{http://example.com/2009/ns1}foo' mit dem Wert 'a'
fehlgeschlagen.

so for the first validate call there is no error found (errorCode is 0)
and for the second validate call the problem is found.

Unfortunately if I change the code to use MSXML 5

Const ns1 = "http://example.com/2009/ns1"
Dim doc : Set doc = CreateObject("Msxml2.DOMDocument.5.0")
Dim root : Set root = doc.createNode(1, "root", ns1)
Dim foo : Set foo = doc.createNode(1, "foo", ns1)
foo.text = "1"
root.appendChild foo
doc.appendChild root

Dim schemas : Set schemas = CreateObject("Msxml2.XMLSchemaCache.5.0")
schemas.add ns1, "test2009011401Xsd.xml"
Set doc.schemas = schemas

Dim error : Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason

Set foo = doc.createNode(1, "foo", ns1)
foo.text = "a"
root.appendChild foo
Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason

then the result is (messages are in German as I am on a German Windows XP)

-1072897500: Der Knoten ist weder gültig noch ungültig, weil keine
DTD/Schema-Deklaration vorhanden ist.

-1072897500: Der Knoten ist weder gültig noch ungültig, weil keine
DTD/Schema-Deklaration vorhanden ist.

So MSXML 5 complains it does not have a schema or DTD for the node
although the schemas property has been set.

I am not sure how to fix that other than to suggest to use MSXML 6
instead of MSXML 5.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
MartinL
2009-01-14 13:19:01 UTC
Permalink
Hi Martin,

Thanks for your help. I used MSXML6 and was able to successfully validate
the XML before writing it out.

Regards,
Martin
Post by Martin Honnen
Post by MartinL
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?
I have now looked at the details and MSXML 5 and MSXML 6 behave
unfortunately quite different.
With MSXML 6 and the validate method I get the correct result with the
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/2009/ns1"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="foo" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Const ns1 = "http://example.com/2009/ns1"
Dim doc : Set doc = CreateObject("Msxml2.DOMDocument.6.0")
Dim root : Set root = doc.createNode(1, "root", ns1)
Dim foo : Set foo = doc.createNode(1, "foo", ns1)
foo.text = "1"
root.appendChild foo
doc.appendChild root
Dim schemas : Set schemas = CreateObject("Msxml2.XMLSchemaCache.6.0")
schemas.add ns1, "test2009011401Xsd.xml"
Set doc.schemas = schemas
Dim error : Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason
Set foo = doc.createNode(1, "foo", ns1)
foo.text = "a"
root.appendChild foo
Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason
outputs (error messages are in German as I am on a German Windows XP)
-1072897535: Fehler beim Analysieren von 'a' als int-Datentyp.
Analyse des Elements '{http://example.com/2009/ns1}foo' mit dem Wert 'a'
fehlgeschlagen.
so for the first validate call there is no error found (errorCode is 0)
and for the second validate call the problem is found.
Unfortunately if I change the code to use MSXML 5
Const ns1 = "http://example.com/2009/ns1"
Dim doc : Set doc = CreateObject("Msxml2.DOMDocument.5.0")
Dim root : Set root = doc.createNode(1, "root", ns1)
Dim foo : Set foo = doc.createNode(1, "foo", ns1)
foo.text = "1"
root.appendChild foo
doc.appendChild root
Dim schemas : Set schemas = CreateObject("Msxml2.XMLSchemaCache.5.0")
schemas.add ns1, "test2009011401Xsd.xml"
Set doc.schemas = schemas
Dim error : Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason
Set foo = doc.createNode(1, "foo", ns1)
foo.text = "a"
root.appendChild foo
Set error = doc.validate
WScript.Echo error.errorCode & ": " & error.reason
then the result is (messages are in German as I am on a German Windows XP)
-1072897500: Der Knoten ist weder gültig noch ungültig, weil keine
DTD/Schema-Deklaration vorhanden ist.
-1072897500: Der Knoten ist weder gültig noch ungültig, weil keine
DTD/Schema-Deklaration vorhanden ist.
So MSXML 5 complains it does not have a schema or DTD for the node
although the schemas property has been set.
I am not sure how to fix that other than to suggest to use MSXML 6
instead of MSXML 5.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Continue reading on narkive:
Loading...