XML Spec says that when someone declares xml:lang
somewhere in the ancestor chain of an XML document, element nodes in the DOM are supposed to inherit the value of xml:lang
. However, although xml:lang
is an inherent part of XML, the DOM Core level 3 specs lacks means to easily find what value of xml:lang
an element has inherited (or explicitly has been assigned).
From section 2.12 Language Identification of the XML spec :
The language specified by xml:lang applies to the element where it is specified (including the values of its attributes), and to all elements in its content unless overridden with another instance of xml:lang. In particular, the empty value of xml:lang is used on an element B to override a specification of xml:lang on an enclosing element A, without specifying another language. Within B, it is considered that there is no language information available, just as if xml:lang had not been specified on B or any of its ancestors. Applications determine which of an element’s attribute values and which parts of its character content, if any, are treated as language-dependent values described by xml:lang.
Below is a little useful code snippet to help you find the value of xml:lang
. The code simply recurses up the tree till it finds an xml:lang attribute to inherit the value from. If it can’t find one, it just returns an empty string:
function xmlLang(element){
var xmlns = "http://www.w3.org/XML/1998/namespace";
var value = element.getAttributeNS(xmlns,"lang");
//check if we are at the root
if(element === element.ownerDocument.documentElement){
//no xml:lang?
if(!element.hasAttributeNS(xmlns,"lang")){
return "";
}
//we have it, so return it.
return value;
}
//this is an element in the tree
if(!element.hasAttributeNS(xmlns,"lang")){
//no xml:lang? recurse upwards
return xmlLang(element.parentNode);
}
//we have a value, so return it
return value;
}
To make it more useful, it would be good to validate the value derived from the code above against the IANA language tag registry.