Home > Articles > Web Services > XML

Miscellaneous Tagging in XML

  • Print
  • + Share This
When developing your own XML schema or writing localization tools, it is important to address the different types of information encountered in XML documents. This chapter presents three such types and explains their uses.
This chapter is from the book

Several text properties are common to all types of XML documents, regardless of their final purpose. These characteristics occur so frequently that the various XML standards often include special provisions for them.

These properties cover the following challenges:

  • How to identify the language of the content—For example, how to distinguish a paragraph in German from one in Czech.

  • How to handle white spaces—Basically, how to note the difference between spaces that are meant to make the structure of the document more readable (spaces such as indentations and empty lines), and the ones that are meaningful.

  • How to represent date and time information—In our case especially, how to display this information both from locale-specific and from locale-neutral viewpoints.

Language Settings

To address language identification, XML provides a default attribute to specify the language of any element: xml:lang. In many respects this attribute can also be seen as a locale.

A locale is, roughly, the combination of a language and a region. The classic example is the difference between French, the language, and the two locales: French for France, and Canadian French (Québécois). Many other examples exist: the various flavors of Spanish, Brazilian versus Portuguese, and so forth.

In addition to linguistic differences, the locale also often indicates possible variations on how to process data: Currency, numbers, date/time formatting, sorting, and character uppercasing and lowercasing are some of the locale-specific areas. Sometimes the locale even goes beyond and points to deeper differences such as the type of writing system (for example, Classical Mongolian versus Cyrillic Mongolian, or Azerbaijani Arabic versus Azerbaijani Cyrillic).


A good example of a language where differences are clear between locales is Spanish.

Spanish is spoken in many countries and therefore comes in many different varieties. When localizing for a specific market you must decide which flavor you need.

For example, Spaniards use "utilidades" for "utility programs," Argentines use "utilitiarios," and Mexicans use "utilerías." Another example is the term "computer." Spaniards use the word "ordenador" but all Latin Americans use "computadora" instead. Such discrepancies cause a few dilemmas when you want to have only one Spanish translation for all markets.

To reduce costs, companies often try to use a "neutral" or "international" Spanish. This is an artificial creation, as is "Latin American Spanish."

Finally, to avoid confusion, you might want to refer to the Spanish spoken in Spain as "Iberian Spanish" rather than "Castilian Spanish," the term "Castellano" being often used in South America to refer to the Spanish spoken there.

When defining your own XML vocabulary, you should use xml:lang as your attribute to specify the locale information, rather than come up with your own attribute. There are a couple of good reasons for this.

First, xml:lang will be understood immediately by any XML user. And second, it will allow you to take advantage of interoperability among the various XML-related technologies such as XSL or CSS.

If you use a DTD to specify your format, xml:lang must still be declared, just as with any other attribute. For example:

 xml:lang NMTOKEN #IMPLIED >

Language Codes

The values of the xml:lang attribute should conform to the language tags defined in the XML specifications, as shown in Listing 3.1.

Listing 3.1Definition of the Value for the xml:lang Attribute

LangValue ::= Langcode ('-' Subcode)*
Langcode  ::= ISO639Code | IanaCode | UserCode
ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
IanaCode  ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
UserCode  ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
Subcode  ::= ([a-z] | [A-Z])+

In addition, according to RFC1766, the part on the right of the '-' can be up to 8 characters long.

Currently the language codes use ISO 639 2-letter codes, but as of January 2001, RFC1766 has been superseded by RFC3066, which introduces the use of ISO 639 3-letter codes.

According to this last RFC, if a language can be identified with both types of code, the 2-letter code must be used. The 3-letter codes should be used only for representing languages that do not have 2-letter codes. For example, the code for Korean must always be ko and never kor.

In addition, there are 2 types of 3-letter codes: Terminology and Bibliography. Currently none of the languages that should be using a 3-letter code have a discrepancy between the Terminology form and the Bibliography form. If such a conflict occurs in the future, the Terminology code should be used.

Finally, if a language has both an ISO code and an IANA code, the ISO code must be used.

Table 3.1 Use of ISO Codes



























Normally, attribute values in XML are case sensitive. However, for simplification purposes and to match the RFC3066 standard, the values of the xml:lang attribute are not case sensitive. For example, the four values "pt-BR", "PT-BR", "pt-br", and "PT-br" (Brazilian Portuguese) are considered identical.

Usually the language code is represented in lowercase and the country code in uppercase, but this is not a rule.

User-Defined Codes

In some cases the list of variant codes you can build from the predefined language and region codes is not enough.

For instance, as we have seen already, you might have to localize a document in two types of Spanish: one for the audience in Spain (Iberian Spanish) and the other for the Latin American market. The first should be coded "es-ES", or simply "es" because Spain is the default country for Spanish. For the second, however, no country code corresponds to "Latin America." To solve this you can create your own locale codes as defined by UserCode in Listing 3.1. For example, you could use something such as "x-es-LatAm" for your Latin-American Spanish document.

A special kind of user-defined code exists: the one registered to the IANA. Most of them start with the prefix i-. The list of these language tags is updated regularly and you can find it at http://www.iana.org/assignments/languages.


Be aware that some localization tools might be programmed to handle only 4-letter codes, and might not be able to process IANA or user-defined codes correctly.

For a detailed list of language codes, see Appendix D.

Multilingual Documents

As you saw in Chapter 2, "Character Representation," one characteristic of XML is its capability to handle content in different languages when necessary.

For example, as shown in Listing 3.2, a SOAP data file could store description of an item in several languages.

Listing 3.2 Soap1.xml—SOAP Envelope with Multilingual Entries

<!-- SOAP excerpt -->
 <d:GetItem xmlns:d="uri:myData" xml:lang="en">
  <d:Desc>Manual water pump</d:Desc>
  <d:Desc xml:lang="fr">Pompe à eau manuelle</d:Desc>
  <d:Desc xml:lang="ja">éËìÆÉEÉHÅ[É^Å[ÅEÉ|ÉìÉv</d:Desc>

The default language from the <d:GetItem> element level is set to en (English). The child elements inherit the property, so the first <d:Desc> element does not need to repeat the attribute. However, because the second one contains the description in French, you need to override the default xml:lang attribute.

Always keep in mind that XML element and attribute names can have non-ASCII characters as well. In such occurrences, the language specifications work the same. Listing 3.3 shows the same SOAP envelope, but this time with the user data marked up with a Russian vocabulary. The data are identical and the xml:lang mechanism is expected to behave the same: It applies to the content, not to the tags.

Listing 3.3 Soap2.xml—SOAP Envelope with Multilingual Entries and Some Non-ASCII Elements

<!-- SOAP excerpt -->
  <_:________>Manual water pump</_:________>
  <_:________ xml:lang="fr">Pompe à eau manuelle</_:________>
  <_:________ xml:lang="ja">éËìÆÉEÉHÅ[É^Å[ÅEÉ|ÉìÉv</_:________>

Note the value of the xmlns attribute: The namespace prefix _ is associated to a URI reference (_________), but here the URI has already been coded into its UTF-8/escaped form as described in Chapter 2.

The lang Attribute in XHTML

For historical reasons, in addition to xml:lang, XHTML also allows the attribute lang to specify language switch. Both have exactly the same significance.

In case the same element has both xml:lang and lang with two different values, xml:lang takes precedence over lang.


Using xml:lang or lang has no direct impact on the way the text is rendered. For example, specifying a paragraph as Arabic does not trigger right-to-left display. You must use the style sheets and the various internationalization elements and attributes such as <bdo>, <dir>, and <ruby> for XHTML to indicate to the user-agent how the text should be displayed.

However, take into account that language is important in some cases: for example, to select an appropriate font. If a document is encoded in UTF-8 or UTF-16, there is no easy way to distinguish Chinese from Japanese, because most ideographs have been unified.

The lang() Function in XPath

XPath is the language used in various XML applications to specify a "path notation" that allows you to navigate through the hierarchical structure of any XML document. It is also used to test whether the node of a document instance matches a given pattern. XPath is used, for example, in conjunction with XPointer and XSLT.

XPath designers have wisely provided a function to match languages: lang().

The function uses the xml:lang attribute to match a given parameter. This is very useful because, following the XML specifications, the function is not case sensitive and allows you to match a language value very simply.

When you specify only a language code rather than a locale code (for example, en versus en-GB), the function returns true for any attributes where the first part of its value matches the argument. The separator between both parts of the value is '-'. Consider the following XSL statement:

<xsl:for-each select="lang('es')">

When this command is used on the XML document shown in Listing 3.4, it will return true for all the following elements:

<p xml:lang="es">Spanish text</p>
<p xml:lang="ES">Spanish text</p>
<p xml:lang="es-ES">Iberian Spanish text</p>
<p xml:lang='es-mx'>Mexican Spanish text</p>

Listing 3.4 Spanish.xml— Multilingual Document with Different Spanish Flavors

<?xml version="1.0" ?>
 <p xml:lang="es">Spanish text</p>
 <p xml:lang="fr">French text</p>
 <p xml:lang="ES">Spanish text</p>
 <p xml:lang="CA-es">Catalan text</p>
 <p xml:lang="es-ES">Iberian Spanish text</p>
 <p xml:lang="es-mx">Mexican Spanish text</p>

Keep in mind that not all XSL processors support all XSL features yet. The lang() function is not supported in all browsers, for example.

  • + Share This
  • 🔖 Save To Your Account