xml:lang

xml:lang and Language Identification

xml:lang states the natural language of an element's content as a BCP 47 tag (en, fr, pt-BR, zh-Hant). It is inherited by descendants until one sets its own, and xml:lang="" means "unstated". XPath's lang() tests the inherited value, matching subtags from the left and ignoring case: lang("en") is true for en-US, but lang("en-US") is false for plain en:

Selecting by inherited language with lang()
xquery -s:booknest-tour.xml -qs:'//title[lang("fr")]/string(),
  //contributor[lang("en")]/string(), count(//*[lang("en-US")])'
Output
"Sel et safran"
"Priya Nair"
0

The contributor inherits en from the root. Multilingual titles and descriptions are common in book-trade metadata, and xml:lang is how XSLT picks one and full-text search picks a stemmer (Full-Text Search with XQFT). When you load records into tables, store the language as an explicit column: inheritance vanishes once an element leaves its ancestors.


xml lang

This special attribute declares the formal language for the content of the tag. It can be overridden.
ch02-xml-lang-example.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<title xml:lang="en">Harry Potter</title>