Connexions

You are here: Home » Content » Combining XML Languages
Content Actions

Combining XML Languages

Module by: Sarah Coppin, Brent Hendricks

Summary: This module explains how to use XML namespaces and DTDs to combine multiple XML languages in the same document.

XML allows you to create documents in custom markup languages. But what if you want to combine markup from multiple languages in the same document? What if there are one or more tags that exist in both languages, but with different meanings? You could, for example, have a <table> tag in HTML and one in a language describing office furniture as well. How do you use these tags unambiguously, without losing functionality?
The solution is to use an extension to XML called namespaces (See the W3C's recommendation, Namespaces in XML). A namespace associates a unique global identifier (usually a URI) with a particular set of tags and their usage rules. To declare a namespace for a particular tag, set the xmlns attribute to the value of the unique identifier.
You can also define a namespace prefix for use in your document. To do this, use a modified version of the xmlns attribute. For example, you would use the attribute xmlns:foo="http://somewhere.org/foo" to associate the prefix foo with the namespace identifier http://somewhere.org/foo. You can then indicate which tags come from that namespace by adding the appropriate prefix to each tag. Thus, the bar tag in foo's namespace would be written as <foo:bar> and </foo:bar>.
When you use the default namespace any children of that tag lacking an explicit prefix will be assumed to have come from the same namespace. This allows you to define a default namespace for all of the children of a tag. This is especially useful when used on the root node, which is the outermost tag in a document.
Example 1 
The namespace identifier for version 0.5 of CNXML is http://cnx.rice.edu/cnxml. Thus to declare CNXML as the default namespace for a document, you would add the attribute to the document tag. Similarly, the namespace identifier for MathML is http://www.w3.org/1998/Math/MathML, so to associate the MathML namespace with the prefix m, add the attribute xmlns:m="http://www.w3.org/1998/Math/MathML" to the document tag.
Namespaces allow you to write documents in multiple languages at the same time. However, in order for these documents to be valid, they must have a Document Type Definition (DTD) that allows tags from one language to exist inside tags from another language. So if you want to include MathML in a CNXML document, you would need a DTD that allowed MathML to exist inside certain CNXML tags. Once that DTD existed, you would declare the DOCTYPE the same as normal.
Example 2 
For just CNXML, the doctype declaration is:
    <!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5//EN" 
              "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_plain.dtd">
      
For CNXML and MathML, the doctype declaration is:
    <!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" 
              "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
      
For CNXML and QML, the doctype declaration is:
    <!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus QML//EN" 
              "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_qml.dtd">
      
For CNXML, QML, and MathML, the doctype declaration is:
    <!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus
              MathML plus QML//EN" 
              "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml_qml.dtd">
      
Here is a simple document with both CNXML and MathML. Notice that the CNXML namespace is the default and the MathML namespace has the m prefix.
<?xml version="1.0" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" 
              "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">

      <document xmlns="http://cnx.rice.edu/cnxml"
              xmlns:m="http://www.w3.org/1998/Math/MathML"
              . . .>

       <metadata xmlns:md="http://cnx.rice.edu/mdml/0.4">
  <md:version>2.14</md:version>
  <md:created>2001/06/29</md:created>
  <md:revised>2004-04-19T19:09:52Z</md:revised>
  <md:authorlist>
    <md:author id="selc">
      <md:firstname>Sarah</md:firstname>
      
      <md:surname>Coppin</md:surname>
      <md:email>coppin@alumni.rice.edu</md:email>
    </md:author>
    <md:author id="brentmh">
      <md:firstname>Brent</md:firstname>
      <md:othername>Michael</md:othername>
      <md:surname>Hendricks</md:surname>
      <md:email>brentmh@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="mizar">
      <md:firstname>Christine</md:firstname>
      
      <md:surname>Donica</md:surname>
      <md:email>mizar@alumni.rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="selc">
      <md:firstname>Sarah</md:firstname>
      
      <md:surname>Coppin</md:surname>
      <md:email>coppin@alumni.rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="jenn">
      <md:firstname>Jenn</md:firstname>
      <md:othername>A.</md:othername>
      <md:surname>Drummond</md:surname>
      <md:email>jenn@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="brentmh">
      <md:firstname>Brent</md:firstname>
      <md:othername>Michael</md:othername>
      <md:surname>Hendricks</md:surname>
      <md:email>brentmh@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>XML</md:keyword>
    <md:keyword>namespace</md:keyword>
    <md:keyword>DTD</md:keyword>
    <md:keyword>tutorial</md:keyword>
  </md:keywordlist>

  <md:abstract>This module explains how to use XML namespaces and DTDs to combine multiple XML languages in the same document.</md:abstract>
</metadata>

       <content>
        <para id='p1'>
         This math says "3y".
         <m:math display='inline'>
          <m:apply>
           <m:times/>
           <m:cn>3</m:cn>
           <m:ci>y</m:ci>
          </m:apply>
         </m:math>
        </para>
       </content>

      </document>
So, by combining the use of namespaces and carefully written DTD's, you can write documents that use multiple XML languages at the same time.

Comments, questions, feedback, criticisms?

Send feedback