Namespace Introduction

  • XML namespaces provide a simple method for qualifying element and attribute names used in Extensible Markup Language documents by associating them with namespaces identified by URI references.

  • Namespace URI

    The namespace value is a URI reference (Uniform Resource Indicator) this does not mean that the resource should be resolvable like a URL.

  • Example:

    We have 2 different XML Vocabularies which can be used together but are using the same element names. XHTML uses the <select>, <submit> and <input> elements to define input controls, however XForms also allows these 2 elements. Namespaces can now be used to distinguish between these elements.

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
        <title>XForms Example</title>
        <model xmlns="http://www.w3.org/2002/xforms">
          <instance>
            <person xmlns="">
              <fname/>
              <lname/>
            </person>
          </instance>
          <submission id="form1" method="get" action="submit.asp"/>
        </model>  
      </head>
      <body>
        <p>The following is XForms markup:</p>
        <input ref="fname" xmlns="http://www.w3.org/2002/xforms">
          <label>First Name</label>
        </input>
        <br/>
        <input ref="lname" xmlns="http://www.w3.org/2002/xforms">
          <label>Last Name</label>
        </input>
        <br/>
        <br/>
        <submit submission="form1" xmlns="http://www.w3.org/2002/xforms">
          <label>Submit</label>
        </submit>
      </body>
    </html>					
    					
  • Constraints:

    The prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace.

    Namespace declarations must be provided either directly or via default attributes declared in the internal subset of the DTD.

    Note

    Never use the "default attributes declared in a DTD" mechanism, this makes documents very dependent on the DTD.
  • http://www.w3.org/TR/REC-xml-names/