lang() Function | |
Determines whether a given language string is the same as, or is a sublanguage of, the language of the context node, as defined by an xml:lang attribute. | |
Inputs | |
A string representing a language code. If the context node has a language of xml:lang="en-us", invoking the lang() function with any of the values en, EN, and en-us returns the boolean value true, while invoking lang() with the value en-gb returns the boolean value false. |
|
Output | |
If the argument string is the same as, or is a sublanguage of, the context node's language, lang() returns the boolean value true. If the context node does not have an xml:lang attribute, then the value of the xml:lang attribute of its nearest ancestor is used instead. If there is no such attribute, then the lang() function returns the boolean value false. When comparing the language code of the context node with the argument string, the lang() function ignores case. |
|
Defined in | |
XPath section 4.3, Boolean Functions. |
|
Example | |
Here is an XML document that uses language codes: <?xml version="1.0"?> <list xml:lang="en"> <title>Albums I've bought recently:</title> <listitem>The Sacred Art of Dub</listitem> <listitem>Only the Poor Man Feel It</listitem> <listitem>Excitable Boy</listitem> <listitem xml:lang="sw">Aki Special</listitem> <listitem xml:lang="en-gb">Combat Rock</listitem> <listitem xml:lang="zu">Talking Timbuktu</listitem> <listitem xml:lang="jz">The Birth of the Cool</listitem> </list> Here's a stylesheet that uses the lang() function: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:for-each select="list/listitem"> <xsl:choose> <xsl:when test="lang('EN')"> <xsl:text>Here's an English-language album: </xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>-------> Here's some World music: </xsl:text> </xsl:otherwise> </xsl:choose> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> Finally, here are the results: Here's an English-language album: The Sacred Art of Dub Here's an English-language album: Only the Poor Man Feel It Here's an English-language album: Excitable Boy -------> Here's some World music: Aki Special Here's an English-language album: Combat Rock -------> Here's some World music: Talking Timbuktu -------> Here's some World music: The Birth of the Cool |