PRB: Namespace Prefixes Are Declared as FIXED Attributes in an XML DTD

Symptoms

The Microsoft XML (MSXML) parser does not interpret namespace prefixes that are declared as FIXED attributes in an XML Document Type Definition (DTD). If you use a FIXED value attribute in a DTD to declare a namespace prefix, and if you use the namespace prefix to qualify element or attribute names without explicitly declaring the prefix in an XML document that references the DTD, you may receive the following error message when the MSXML parser tries to load the XML:

Reference to undeclared namespace prefix: <Namespace prefix>
This error occurs whether you program the MSXML Document Object Model (DOM) through code or open the XML document in Microsoft Internet Explorer.

Cause

This problem occurs because XML Language and Namespace specifications do not define how DTDs and namespaces should interact with each other.

Resolution

To work around this problem, explicitly declare the namespace prefix in the XML document before you use it to qualify element and/or attribute names. For more information and sample code, see the "More Information" section.

More Information

Some third party XML parsers may be able to interpret and load XML documents that use namespace prefixes that are defined in a referenced DTD as a FIXED value attribute without explicitly declaring them. However, this does not mean that the behavior of such parsers is always correct. The correct behavior of an XML parser is defined by the World Wide Web Consortium (W3C) XML specifications. Currently, the XML Language and Namespace specifications do not define how DTDs and namespaces should interact with each other.

Steps to Reproduce Behavior

To create an XML document with an inline DTD and reproduce this behavior, follow these steps:

  1. In Notepad, open a new text file.
  2. Copy and paste the following XML code:
    <?xml version="1.0"?>
    <!DOCTYPE Books [
    <!ELEMENT Books (Book+)>
    <!ATTLIST Books
    xmlns:Bookns CDATA #FIXED "http://myserver/books"
    >
    <!ELEMENT Book EMPTY>
    <!ATTLIST Book
    Bookns:ISBN CDATA #REQUIRED
    Bookns:TITLE CDATA #REQUIRED
    >
    ]>
    <Books>
    <Book Bookns:ISBN="031-789-098" Bookns:TITLE="ADO 2.6 Programmers Reference"/>
    <Book Bookns:ISBN="098-876-654" Bookns:TITLE="Professional VB6 XML"/>
    </Books>
  3. Save this file as Books.xml.
  4. In Internet Explorer, open Books.xml. The MSXML parser generates the following error message:
    Reference to undeclared namespace prefix: 'Bookns'. Line 14, Position 81
  5. Program the MSXML DOM to load Books.xml. Notice that you receive the same error message. To access the error message, use the reason property of the parseError object property of the MSXML DOMDocument object.

Workaround

This error occurs because the namespace prefix Bookns is used to prefix the ISBN and TITLE attributes of the Book element and is not explicitly declared in the XML portion of the document. The sample document that you created in Step 2 contains an inline DTD that defines the Bookns namespace prefix as a FIXED attribute of the Books element. However, this declaration alone is not sufficient for the MSXML parser to correctly interpret and load the XML. You must explicitly declare the namespace prefix in the XML document before you can use it to qualify element and/or attribute names.

To work around this behavior, replace the opening <Books> element in the XML with the following line of code to explicitly declare the Bookns namespace prefix attribute as specified in the DTD:

<Books xmlns:Bookns="http://myserver/books">
Save the change to Books.xml, and then open Books.xml in Internet Explorer. Notice that the browser loads and displays the XML correctly.

NOTE This behavior also occurs in an XML document that references an external DTD.
Propriedades

ID do Artigo: 296492 - Última Revisão: 4 de ago de 2008 - Revisão: 1

Comentários