key() 関数 | |
<xsl:key> 要素で定義された関係を参照します。概念上、key() 関数は id() 関数と同様に動作しますが、この関数はより ID よりも柔軟性が高くなっています。 | |
入力 | |
<xsl:key> 要素によって定義されるキーとオブジェクトの名前。オブジェクトがノードセットである場合、key() 関数はノードセットの各ノードの文字列値にそれ自体を適用し、これらすべての key() 関数呼び出しの結果のノードセットを返します。オブジェクトが別のタイプである場合は、string() 関数を呼び出したかのように文字列に変換されます。 |
|
出力 | |
要求したキーが検索パラメータと一致する値のコンテキストノードと同じドキュメントのノードを含むノードセット。つまり、現在のドキュメントのすべての <address> 要素の <postalcode> 子に基づく、postalcodes という名前のキーを定義する <xsl:key> 要素がスタイルシートにある場合、関数呼び出し key(postalcodes, '34829') は、値が 34829 である <postalcode> 要素を持つすべての <address> 要素が含まれたノードセットを返します。 |
|
定義先 | |
XSLT 12.2 節「キー」 |
|
例 | |
key() 関数の強力な機能を示すために、第 5 章で説明した用語集の、このドキュメント用の省略バージョンを使用します。 <?xml version="1.0" ?> <glossary> <glentry> <term id="applet">applet</term> <defn topic="Java" language="en"> An application program, written in the Java programming language, that can be retrieved from a web server and executed by a web browser. A reference to an applet appears in the markup for a web page, in the same way that a reference to a graphics file appears; a browser retrieves an applet in the same way that it retrieves a graphics file. For security reasons, an applet's access rights are limited in two ways: the applet cannot access the filesystem of the client upon which it is executing, and the applet's communication across the network is limited to the server from which it was downloaded. Contrast with <xref refid="servlet"/>. </defn> <defn topic="Java" language="it"> [Pretend this is an Italian definition of applet.] </defn> <defn topic="Java" language="es"> [Pretend this is a Spanish definition of applet.] </defn> </glentry> <glentry> <term id="DMZlong" xreftext="demilitarized zone">demilitarized zone (DMZ)</term> <defn topic="security" language="en"> In network security, a network that is isolated from, and serves as a neutral zone between, a trusted network (for example, a private intranet) and an untrusted network (for example, the Internet). One or more secure gateways usually control access to the DMZ from the trusted or the untrusted network. </defn> <defn topic="security" language="it"> [Pretend this is an Italian definition of DMZ.] </defn> <defn topic="security" language="es"> [Pretend this is a Spanish definition of DMZ.] </defn> <defn topic="security" language="jp"> [Pretend this is a Japanese definition of DMZ.] </defn> <defn topic="security" language="de"> [Pretend this is a German definition of DMZ.] </defn> </glentry> <glentry> <term id="servlet">servlet</term> <defn topic="Java" language="en"> An application program, written in the Java programming language, that is executed on a web server. A reference to a servlet appears in the markup for a web page, in the same way that a reference to a graphics file appears. The web server executes the servlet and sends the results of the execution (if there are any) to the web browser. Contrast with <xref refid="applet" />. </defn> <defn topic="Java" language="es"> [Pretend this is a Spanish definition of servlet.] </defn> <defn topic="Java" language="it"> [Pretend this is an Italian definition of servlet.] </defn> <defn topic="Java" language="de"> [Pretend this is a German definition of servlet.] </defn> <defn topic="Java" language="jp"> [Pretend this is a Japanese definition of servlet.] </defn> </glentry> </glossary> このドキュメントの処理に使用するスタイルシートを次に示します。ここでは、2 つの <xsl:key> 要素を定義して XML ドキュメントに 2 つの異なる方法でインデックスを付けます。 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="language-index" match="defn" use="@language"/> <xsl:key name="term-ids" match="term" use="@id"/> <xsl:param name="targetLanguage"/> <xsl:template match="/"> <xsl:apply-templates select="glossary"/> </xsl:template> <xsl:template match="glossary"> <html> <head> <title> <xsl:text>Glossary Listing: </xsl:text> </title> </head> <body> <h1> <xsl:text>Glossary Listing: </xsl:text> </h1> <xsl:for-each select="key('language-index', $targetLanguage)"> <xsl:apply-templates select="ancestor::glentry"/> </xsl:for-each> </body> </html> </xsl:template> <xsl:template match="glentry"> <p> <b> <a> <xsl:attribute name="name"> <xsl:value-of select="term/@id" /> </xsl:attribute> </a> <xsl:value-of select="term"/> <xsl:text>: </xsl:text> </b> <xsl:apply-templates select="defn[@language=$targetLanguage]"/> </p> </xsl:template> <xsl:template match="defn"> <xsl:apply-templates select="*|comment()|processing-instruction()|text()"/> </xsl:template> <xsl:template match="xref"> <a> <xsl:attribute name="href"> <xsl:text>#</xsl:text><xsl:value-of select="@refid"/> </xsl:attribute> <xsl:choose> <xsl:when test="key('term-ids', @refid)[1]/@xreftext"> <xsl:value-of select="key('term-ids', @refid)[1]/@xreftext"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="key('term-ids', @refid)[1]"/> </xsl:otherwise> </xsl:choose> </a> </xsl:template> </xsl:stylesheet> targetLanguage に en を指定して用語集を変換した結果は次のとおりです。 <html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Glossary Listing: </title> </head> <body> <h1>Glossary Listing: </h1> <p> <b><a name="applet"></a>applet: </b> An application program, written in the Java programming language, that can be retrieved from a web server and executed by a web browser. A reference to an applet appears in the markup for a web page, in the same way that a reference to a graphics file appears; a browser retrieves an applet in the same way that it retrieves a graphics file. For security reasons, an applet's access rights are limited in two ways: the applet cannot access the filesystem of the client upon which it is executing, and the applet's communication across the network is limited to the server from which it was downloaded. Contrast with <a href="#servlet">servlet</a>. </p> <p> <b><a name="DMZlong"></a>demilitarized zone (DMZ): </b> In network security, a network that is isolated from, and serves as a neutral zone between, a trusted network (for example, a private intranet) and an untrusted network (for example, the Internet). One or more secure gateways usually control access to the DMZ from the trusted or the untrusted network. </p> <p> <b><a name="servlet"></a>servlet: </b> An application program, written in the Java programming language, that is executed on a web server. A reference to a servlet appears in the markup for a web page, in the same way that a reference to a graphics file appears. The web server executes the servlet and sends the results of the execution (if there are any) to the web browser. Contrast with <a href="#applet">applet</a>. </p> </body> </html> 図 C-5 に、このドキュメントをブラウザで表示した外観を示します。targetLanguage に jp を指定した結果は次のとおりです。 生成された HTML 用語集 <html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Glossary Listing: </title> </head> <body> <h1>Glossary Listing: </h1> <p> <b><a name="DMZlong"></a>demilitarized zone (DMZ): </b> [Pretend this is a Japanese definition of DMZ.] </p> <p> <b><a name="servlet"></a>servlet: </b> [Pretend this is a Japanese definition of servlet.] </p> </body> </html> </programlisting> このドキュメントの表示を図 C-6 に示します。targetLanguage を変更すると、結果がまったく異なることがわかります。 生成された HTML 用語集 |