This is an XSLT Document that demonstrates how to determine the correct value for an xml:lang attribute for an element using only XPath 1.0.
This code is all released directly into the Public Domain, but it would be swell if you could provide attribution.
Sam Spencer - theodore[dot]therone[at]gmail[dot]com
The XPath just below (and also on line 8 of xml_lang_xpath_resolver.xsl) can be used to find the correct value of an xml:lang attribute for a given element.
ancestor-or-self::*[attribute::xml:lang][1]/@xml:lang
Broken down this (The decribed pieces at each stage are underlined using carets.
-
Follows the tree back to the root, through all ancestors, and returns only those that have an xml:lang attribute.
ancestor-or-self::*[attribute::xml:lang][1]/@xml:lang ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
This list is returned in order going away the element we are searching from, so we grab the first (or closest element).
ancestor-or-self::*[attribute::xml:lang][1]/@xml:lang ^^^
-
Having the nearest element that has an xml:lang set, we than just return the value of its xml:lang attribute.
ancestor-or-self::*[attribute::xml:lang][1]/@xml:lang ^^^^^^^^^^
It will clone an XML document exactly and add the correct xml:lang tag into the document. In reality, this is probably not something that you would want to do, but this serves to demonstrate what the xml:lang attribute should be when processing an XML document. This is an extension of the XSLT Identity Template which can be read about here: http://www.xmlplease.com/xsltidentity.
Ignore all of the prior changes, i just don't get Markdown.