XSLT – テキストの生成 (xsl:text)
書式
<xsl:text>
テキスト文字列
<xsl:/text>
例
<xsl:text>
名前
<xsl:/text>
「名前」 というテキストが作成される。
サンプルコード
sample.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="./style.xsl"?>
<document>
<book id="10">
<title>AAA</title>
<price>4000</price>
</book>
<book id="20">
<title>BBB</title>
<price>1000</price>
</book>
<book id="30">
<title>CCC</title>
<price>2000</price>
</book>
<book id="40">
<title>DDD</title>
<price>1500</price>
</book>
<book id="50">
<title>EEE</title>
<price>500</price>
</book>
</document>
style.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="document">
<html>
<head>
<title>XSLT</title>
</head>
<body>
<xsl:apply-templates select="book"/>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<xsl:text>タイトル:</xsl:text>
<xsl:value-of select="title"/><br/>
<xsl:text>値段:</xsl:text>
<xsl:value-of select="price"/><br/><br/>
</xsl:template>
</xsl:stylesheet>
生成されるコード
<html> <head> <title>XSLT</title> </head> <body> タイトル:AAA<br /> 値段:4000<br /> <br /> タイトル:BBB<br /> 値段:1000<br /> <br /> タイトル:CCC<br /> 値段:2000<br /> <br /> タイトル:DDD<br /> 値段:1500<br /> <br /> タイトル:EEE<br /> 値段:500<br /> <br /> </body> </html>
XSLT – 値の参照 (xsl:value-of)
書式
<xsl:value-of select="値を取得するノード"/>
指定したノードの値を取得する。
例
<xsl:value-of select="."/>
サンプルコード
sample.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="./style.xsl"?>
<document>
<title>XSLT</title>
<body>XSLT&XML</body>
</document>
style.xsl
<?xml version="1.0" encoding="shift_jis"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="document">
<html>
<head>
<title>XSLT</title>
</head>
<body>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="body"/>
</body>
</html>
</xsl:template>
<xsl:template match="title">
<h1>
<xsl:value-of select="."/>
</h1>
</xsl:template>
<xsl:template match="body">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
生成されるコード
<html> <head> <title>XSLT</title> </head> <body> <h1>XSLT</h1> <p>XSLT&XML</p> </body> </html>
XSLT – 変数の利用(xsl:variable)
書式
変数の宣言。
<xsl:variable name="変数名">値</xsl:variable> <xsl:variable name="変数名" select="式" />
宣言した変数を使用する際は $ 記号を使用する。
$変数名
例
<!-- 変数宣言 --> <xsl:variable name="title">タイトル:</xsl:variable> <!-- 変数の使用 --> <xsl:value-of select="$title" />
サンプルコード
sample.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="./style.xsl"?>
<document>
<book id="001">
<title>AAA</title>
<price>4000</price>
</book>
<book id="002">
<title>BBB</title>
<price>1000</price>
</book>
<book id="003">
<title>CCC</title>
<price>2000</price>
</book>
<book id="004">
<title>DDD</title>
<price>1500</price>
</book>
<book id="005">
<title>EEE</title>
<price>500</price>
</book>
</document>
style.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="title">タイトル:</xsl:variable>
<xsl:template match="title">
<h1>
<xsl:value-of select="$title"/>
<xsl:value-of select="."/>
</h1>
</xsl:template>
<xsl:template match="price">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
生成されるコード
<h1>タイトル:AAA</h1> <p>4000</p> <h1>タイトル:BBB</h1> <p>1000</p> <h1>タイトル:CCC</h1> <p>2000</p> <h1>タイトル:DDD</h1> <p>1500</p> <h1>タイトル:EEE</h1> <p>500</p>

