XPath/XQuery lang function

Summary

This function tests whether the language of $node, or the context item if the second argument is omitted, as specified by xml:lang attributes is the same as, or is a sublanguage of, the language specified by $testlang.

Signatures

fn:lang(
$testlang as xs:string?
) as xs:boolean
fn:lang(
$testlang as xs:string?,
$node as node()
) as xs:boolean

Properties

The one-argument form of this function is deterministic, context-dependent, and focus-dependent.
The two-argument form of this function is deterministic, context-independent, and focus-independent.

Rules

The behavior of the function if the second argument is omitted is exactly the same as if the context item (.) had been passed as the second argument.

The language of the argument $node, or the context item if the second argument is omitted, is determined by the value of the xml:lang attribute on the node, or, if the node has no such attribute, by the value of the xml:lang attribute on the nearest ancestor of the node that has an xml:lang attribute. If there is no such ancestor, then the function returns false.

If $testlang is the empty sequence it is interpreted as the zero-length string.

The relevant xml:lang attribute is determined by the value of the XPath expression:

(ancestor-or-self::*/@xml:lang)[last()]

If this expression returns an empty sequence, the function returns false.

Otherwise, the function returns true if and only if, based on a caseless default match as specified in section 3.13 of , either:

  1. $testlang is equal to the string-value of the relevant xml:lang attribute, or

  2. $testlang is equal to some substring of the string-value of the relevant xml:lang attribute that starts at the start of the string-value and ends immediately before a hyphen, "-" (the character "-" is HYPHEN-MINUS, #x002D).

Examples

The expression fn:lang("en") would return true if the context node were any of the following four elements:

<para xml:lang="en"/>

<div xml:lang="en"><para>And now, and forever!</para></div>

<para xml:lang="EN"/>

<para xml:lang="en-us"/>

The expression fn:lang("fr") would return false if the context node were <para xml:lang="EN"/>

Error Conditions

The following errors may be raised when $arg is omitted:

If the context item is absent, dynamic error

If the context item is not a node, type error .