Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

Special Attributes: xmlns, xml:space, xml:lang, and xml:base

This section briefly discusses several unrelated, special-case attributes that are namespace related.

xmlns

Originally, xmlns was created exclusively as a syntactic mechanism for declaring either a specific namespace (xmlns:foo="URI") or the default namespace (xmlns="URI") and therefore it did not itself belong to any namespace. However, some application contexts (e.g., the DOM) require all XML attributes to be represented as pairs (namespace name and local name). In those contexts, the namespace name http://www.w3.org/2000/xmlns/ is assigned. Note, however, that only xmlns can be used to declare namespaces; no other prefix will work.

xml:space

The second edition of the XML 1.0 Recommendation describes xml:space and xml:lang, two attributes that can be used with any element. They are discussed here simply because they are related to namespaces.

The prefix xml is by definition bound to the special namespace URI http://www.w3.org/XML/1998/namespace. Other than xmlns itself (which is used strictly to bind a prefix to a URI and isn't actually associated with any namespace except in the context just mentioned), xml is the only namespace prefix that does not need to be declared.

The special attribute named xml:space may be associated with an element to indicate whether white space should be preserved by applications. For validation purposes, this attribute (like all others) must be declared. It can either be fixed as "preserve" or "default," an enumeration of both choices. For example:

<!ATTLIST SourceCode xml:space (default|preserve) 'preserve'>

This attribute-list declaration indicates that the SourceCode element has an attribute named xml:space limited to the values "default" and "preserve," with the latter as the default.

xml:lang

Another special attribute named xml:lang of type NMTOKEN may be used to specify the natural (human) language used in the contents and attribute values of any element in an XML document. Examples of valid NMTOKEN values of this attribute are: "en", "fr", "de", "da", "es", "he", "it", "zh", and "ja", indicating English, French, German, Danish, Spanish, Hebrew, Italian, Chinese, and Japanese, respectively.7 (Sorry, no Klingon yet.) Some languages have country codes or dialects, such as English with "en-GB" for Great Britain and "en-US" for the United States. Suppose we had the DTD declarations:

<!ELEMENT ToDo (ShoppingList+, OtherTask*) >
<!ELEMENT ShoppingList (Item+) >
<!ATTLIST ShoppingList xml:lang NMTOKEN #IMPLIED >
<!ELEMENT Item (#PCDATA) >

Now, using xml:lang we can create a shopping list that is intelligible (although not particularly nutritious) on both sides of the Atlantic:

<ToDo>
 <ShoppingList xml:lang="en-GB">
  <Item>crisps</Item>
  <Item>bangers</Item>
  <Item>biscuits</Item>
  <Item>maize</Item>
  <Item>tin of mince</Item>
 </ShoppingList>
 <ShoppingList xml:lang="en-US">
  <Item>potato chips</Item>
  <Item>sausages</Item>
  <Item>cookies</Item>
  <Item>corn</Item>
  <Item>can of chopped beef</Item>
 </ShoppingList>
</ToDo>

Of course, it's also possible to make xml:lang a fixed value:

<!ATTLIST ShoppingList xml:lang NMTOKEN "en-US" >

xml:base

The final special attribute, xml:base, may be inserted in XML documents to specify a base URI other than the base URI of the document or external entity. The value of this attribute is interpreted as a URI Reference as defined in RFC 2396 [IETF RFC 2396]. XLink uses XML Base to interpret relative URI references in xlink:href attributes. Consider this excerpt from an example we'll later encounter in more detail in the XLink chapter.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE Timeline SYSTEM "timeline10.dtd" >
<Timeline 
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xlink:type="extended" 
 xml:base="http://gernhardt.com/macca/" >

 <Band  xlink:type="locator"
  xlink:label="index"
  xlink:href="wings.html">
  Wings Timeline (1971-1981)
 </Band>
 <Recording  xlink:type="locator" 
  xlink:label="wwl"
  xlink:href="a/wwl.html"
  year="1971">Wings Wild Life
 </Recording>
 <Recording  xlink:type="locator" 
  xlink:label="rrs"
  xlink:href="a/rrs.html"
  year="1973">Red Rose Speedway
 </Recording>
<!-- etc. -->
</Timeline>

If we ignore all details about XLink and focus only on the bold lines, we can see that the Timeline element and all of its children such as Band and Recording are associated with the XLink namespace (or more formally, the namespace name http://www.w3.org/1999/xlink). Timeline has an xml:base attribute which is a URL, (not just a URI). Therefore, for all its child elements, the values of the xlink:href attributes are relative to the base URL. For example, the xlink:href for Band will be interpreted as http://gernhardt.com/macca/wings.html by an XLink processor.

  • + Share This
  • 🔖 Save To Your Account