PRB: MSXML 4.0 Sets the XML Namespace Attribute to an Empty Value for Child Nodes (828928)



The information in this article applies to:

  • Microsoft XML 4.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0

SYMPTOMS

When you add the XML namespace attribute (xmlns) to the root node of an XML document, an XML namespace attribute with an empty value is created for the child nodes of the root node.

CAUSE

You cannot add, modify, or delete an XML namespace definition in an instance of an XML document after the document has been loaded into the XML Document Object Model (XMLDOM) parser. The XML nodes that are used to represent data in the XML document are created when the document is loaded into the XMLDOM parser. These nodes are permanently bound to their XML namespace attributes when they are created. Therefore, the empty XML namespace declaration (xmlns = "") is appended to the child nodes of these nodes to preserve the default XML namespace attribute of these nodes.

WORKAROUND

To work around this problem, specify XML namespace declarations before you load an XML document.

STATUS

This behavior is by design.

MORE INFORMATION

The behavior that is mentioned in the "Symptoms" section of this article is the expected behavior in the MSXML 4.0 Document Object Model (DOM). This behavior also complies with the World Wide Web Consortium (W3C) specifications. The MSXML 4.0 DOM specifies the following clauses:
  • XML namespace definitions are specified at the load time of an XML document.
  • Microsoft does not recommend that you programmatically create or append XML namespace attributes because this may result in inconsistent behavior.
  • The XML nodes that are used to represent data in an XML document are created when the document is loaded into the XMLDOM parser. These XML nodes are permanently bound to their XML namespace attributes when they are created.
  • Adding a default XML namespace attribute to a root node after data is loaded into the XMLDOM parser does not automatically associate all child nodes with the default XML namespace.
  • To dynamically add an element or an attribute node to the XML tree, pass the XML namespace attribute as the third parameter to the CreateNode method.

Steps to Reproduce the Behavior

  1. Paste the following XML code in Notepad, and then save the file as book.xml:
    <?xml version='1.0'?>
    <Collection>
    <Book>
    <Science>
    <Title>Lover Birds</Title>
    <Author>Cynthia Randall</Author>
    <Publisher>Lucerne Publishing</Publisher>
    </Science>
    </Book>
    </Collection>
    
  2. Start Visual Basic 6.0.
  3. Create a Standard EXE project. By default, Form1 is created.
  4. On the Project menu, click References.
  5. In the References - Project1 dialog box, click to select Microsoft XML, v4.0 under Available References, and then click OK.
  6. Add a command button to Form1, and then set the Caption property of the command button to XMLApp.
  7. Add the following code to the Click event of the XMLApp command button:
    Dim xmldom3 As New DOMDocument30
    Dim xmldom4 As New DOMDocument40
    
    xmldom3.Load ("c:\book.xml")
    xmldom3.documentElement.setAttribute "xmlns", "x-schema:books"
    MsgBox ("msxml 3 : " + xmldom3.xml)
    
    xmldom4.Load ("c:\book.xml")
    xmldom4.documentElement.setAttribute "xmlns", "x-schema:books"
    MsgBox ("msxml 4 : " + xmldom4.xml)
    
  8. On the Run menu, click Start. Form1 is displayed.
  9. On the Form1 form, click XMLApp.

    You receive a message box that contains the following content:
    msxml 3 : <?xml version="1.0"?>
    <Collection xmlns="x-schema:books">
    <Book>
    <Science>
    <Title>Lover Birds</Title>
    <Author>Cynthia Randall</Author>
    <Publisher>Lucerne Publishing</Publisher>
    </Science>
    </Book>
    </Collection> The content that is displayed is the output from the MSXML 3.0 parser. Notice that the XML namespace attribute(xmlns = "") is not applied to the child node of the root node.
  10. Click OK to close this message box.

    You receive another message box that contains the following content:
    msxml 4: <?xml version="1.0"?>
    <Collection xmlns="x-schema:books">
    <Book xmlns="">
    <Science>
    <Title>Lover Birds</Title>
    <Author>Cynthia Randall</Author>
    <Publisher>Lucerne Publishing</Publisher>
    </Science>
    </Book>
    </Collection>The content that is displayed is the output from the MSXML 4.0 parser. Notice that the XML namespace attribute (xmlns = "") is applied to the child node of the root node.
  11. Click OK to close this message box.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

290485 DOMDocument.CreateNode() Method Appends an Empty Namespace Declaration

For more information about XML namespaces, visit the following W3C Web site:

Modification Type:MinorLast Reviewed:8/18/2005
Keywords:kbNameSpace kbXML kbprb KB828928 kbAudDeveloper