Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The
xml:lang
attribute defines the language of all text contained within the element it appears on, unless it is overridden by another
xml:lang
attribute in an inner element. So if a document is written in English but contains quotations in German, the
xml:lang
language code on the document element might say
xml:lang=“en”
, while an element containing a quotation specifies
xml:lang=“de”
.
The
lang()
function allows you to test whether the language for the context node is the one you are expecting. For example
lang(‘en’)
returns
true
if the language is English, while
lang (‘jp’)
returns
true
if it is Japanese.
Specifically, the rules are as follows:
Examples
Expression | Result |
boolean(//*[lang(‘de’)]) | true() if the document contains any elements marked as being in German |
/*/msg[@code=$p][lang(‘fr’)] | The |
Usage
This function provides a convenient way of testing the language used in a source document. Assuming that the source document has been properly marked up using the
xml:lang
attribute as defined in the XML specification, the
lang()
function allows you to do language-dependent processing of the data.
The
lang()
function only allows you to test whether the language is one of the languages you are expecting; if you want to find out the actual language, you will need to read the
xml:lang
attribute directly. You can find the relevant attribute using the expression
(ancestor-or-self::*/@xml:lang)[last()]
.
last
The
last()
function returns the value of the context size. When processing a sequence of items, if the items are numbered from one,
last()
gives the number assigned to the last item in the sequence.