lang() 関数  
指定された言語の文字列が、xml:lang 属性によって定義されるコンテキストノードの言語と同じであるか、またはそのサブ言語であるかどうかを調べます。
 
入力

言語コードを表す文字列。コンテキストノードに言語 xml:lang="en-us" がある場合、enEN、および en-us の任意の値を使用して lang() 関数を呼び出すと、ブール値 true が返されます。値 en-gb を使用して lang() を呼び出すと、ブール値 false が返されます。

 
出力

パラメータ文字列が、コンテキストノードの言語と同じであるかそのサブ言語である場合、lang() はブール値 true を返します。コンテキストノードに xml:lang 属性がない場合、最も近い上位の xml:lang 属性の値が代わりに使用されます。このような属性がない場合、lang() 関数はブール値 false を返します。パラメータ文字列でコンテキストノードの言語コードを比較する場合、lang() 関数は大文字小文字を無視します。

 
定義先

XPath 4.3 節「Boolean Functions」

 

言語コードを使用する XML ドキュメントを次に示します。

<?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>

lang() 関数を使用するスタイルシートを次に示します。

<?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>

最後に、この結果は次のとおりです。


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