area1
area1

Reputation: 179

Can someone explain what the xml:lang attribute does in HTML5?

I was wondering what does the xml:lang attribute do and what are it's values in HTML5. and does the xml:lang attribute have any restrictions?

Upvotes: 4

Views: 8492

Answers (4)

You
You

Reputation: 23804

As the standards draft explains in 3.2.3.3 The lang and xml:lang attributes, it is the XML variant of the standard lang attribute, specifying the natural language of the document. It can take any value defined by BCP47. Note however that you may only use the xml:lang attribute if you either have an XML document or also define the lang attribute, and in the latter case they must have the same value. This is because xml:lang is allowed only to ease transition of old XHTML documents:

Authors must not use the lang attribute in the XML namespace on HTML elements in HTML documents. To ease migration to and from XHTML, authors may specify an attribute in no namespace with no prefix and with the literal localname "xml:lang" on HTML elements in HTML documents, but such attributes must only be specified if a lang attribute in no namespace is also specified, and both attributes must have the same value when compared in an ASCII case-insensitive manner.

Upvotes: 6

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

HTML 5 uses the definition in the XML specification. The relevant section is 2.12. It can take a value from BCP 47, or "".

Upvotes: 0

Brian Campbell
Brian Campbell

Reputation: 333066

To understand the xml:lang attribute, you need to be familiar with the two different syntaxes of HTML5, the HTML syntax and the XHTML syntax:

There are various concrete syntaxes that can be used to transmit resources that use this abstract language, two of which are defined in this specification.

The first such concrete syntax is the HTML syntax. This is the format suggested for most authors. It is compatible with most legacy Web browsers. If a document is transmitted with an HTML MIME type, such as text/html, then it will be processed as an HTML document by Web browsers. This specification defines the latest HTML syntax, known simply as "HTML".

The second concrete syntax is the XHTML syntax, which is an application of XML. When a document is transmitted with an XML MIME type, such as application/xhtml+xml, then it is treated as an XML document by Web browsers, to be parsed by an XML processor. Authors are reminded that the processing for XML and HTML differs; in particular, even minor syntax errors will prevent a document labeled as XML from being rendered fully, whereas they would be ignored in the HTML syntax. This specification defines the latest XHTML syntax, known simply as "XHTML".

The DOM, the HTML syntax, and XML cannot all represent the same content. For example, namespaces cannot be represented using the HTML syntax, but they are supported in the DOM and in XML. Similarly, documents that use the noscript feature can be represented using the HTML syntax, but cannot be represented with the DOM or in XML. Comments that contain the string "-->" can only be represented in the DOM, not in the HTML and XML syntaxes.

The HTML syntax is the one that you are likely using; it is much more tolerant of errors, and the XHTML syntax is not supported natively by IE.

In the HTML syntax, the xml:lang attribute is meaningless. It does nothing whatsoever. If you want to set the language, use the lang attribute, which works in the XHTML syntax as well. Using the lang attribute will specify the default language for that element and its children, which may be used for things like picking which font to use (you can use the :lang() CSS selector to control this yourself). In the HTML syntax, it is allowed to include the xml:lang attribute only if the lang attribute is also provided and has the same value; this ensures that no confusion will result from them being different, and the lang attribute is the one the browser will actually pay attention to.

In the XHTML syntax, the xml:lang attribute has semantics defined by the XML specification. When determining the language of a piece of text, in XHTML, the xml:lang attribute takes precedent, but if it is not present, then the lang attribute is used to determine the language (for purposes of font selection and the :lang() CSS selector).

It is best to just use the lang attribute. This will work in the HTML syntax which you are likely to be using, and the XHTML syntax if you wind up using that. If you must process your code with XML tools that do not understand anything about HTML at all, and need the language to be defined, then you can use both the lang attribute and the xml:lang attribute with the same value, to ensure that all tools will see the same language.

The possible values, for both lang and xml:lang, are BCP47 language tags such as en-US for American English, or sr-Latn-RS for Serbian written in a Latin script as used in Serbia. See the BCP47 spec for more details.

Upvotes: 3

user269597
user269597

Reputation:

  1. xml:lang is an XML attribute, and therefore you should only use it in XHTML documents, not HTML documents.

  2. xml:lang specifies which human-readable language the content is written in.

More info: http://www.w3.org/TR/REC-xml/#sec-lang-tag

Upvotes: 1

Related Questions