XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (583 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
13.59Mb size Format: txt, pdf, ePub

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:

  • If the value of
    xml:lang
    for the selected node is equal to the string supplied in the argument, ignoring differences of case, the function returns
    true
    .
  • If the leading part of the value of
    xml:lang
    for the selected node, up to some hyphen
    -
    , is equal to the string supplied in the argument, again ignoring differences of case, the function returns
    true
    .
  • Otherwise, the function returns
    false
    .

Examples

Expression
Result
boolean(//*[lang(‘de’)])
true()
if the document contains any elements marked as being in German
/*/msg[@code=$p][lang(‘fr’)]
The

element with a required code value that is marked as being in French

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.

Other books

A Puzzle for fools by Patrick Quentin
Gawain and Lady Green by Anne Eliot Crompton
Falling by Gordon Brown
Death in Cold Water by Patricia Skalka
Human Rights by S.L. Armstrong
The Bucket List by Gynger Fyer
The Cat at the Wall by Deborah Ellis
Out of India by Michael Foss