XML

よく使われているようだが、よく知らないので作りました。

XSL

XSL(eXtensible Stylesheet Language)は、XMLに対してスタイルを記述します。その中にXSLT(eXtensible Stylesheet Language Transformations)があり、他言語に変換します。
xmlの最初に

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl" ?>

とすると、books.xslによって整形されて出力されます。例として、

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl" ?>
<books title="書籍">
	<owner address="hoge@hoge.com">hidekazu</owner>
	<book isbn="111">
		<name>あああ</name>
		<author>hoge</author>
		<category>java</category>
		<price>1000</price>
		<url>http://test.com</url>
	</book>
	<book isbn="222">
		<name>いいい</name>
		<author>ttt</author>
		<category>ruby</category>
		<price>2000</price>
		<url>http://test2.com</url>
	</book>
	<book isbn="333">
		<name>いいい</name>
		<author>ttt</author>
		<category>vb</category>
		<price>3000</price>
		<url>http://test3.com</url>
	</book>
</books>

というxmlがあり、スタイルシートはbooks.xslを使うので、

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="html" encoding="UTF-8" />
	<xsl:template match="/">
		<html>
			<head>
				<title><xsl:value-of select="books/@title" /></title>
			</head>
			<body>
				<h1>
					<xsl:value-of select="books/@title" />
				</h1>
				<xsl:for-each select="/books/book">
					<xsl:sort select="category" data-type="text" order="ascending" />
					<xsl:value-of select="category" />
					<xsl:element name="a">
						<xsl:attribute name="href">
							<xsl:value-of select="url" />
						</xsl:attribute>
						<xsl:value-of select="author" />
					</xsl:element>
					<xsl:choose>
						<xsl:when test="price[number(.) &lt;= 2000]">
							<b><xsl:value-of select="price" /></b>円
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="price" />円
						</xsl:otherwise>
					</xsl:choose>
					<br />
				</xsl:for-each>
				<xsl:value-of select="books/owner" />
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

とすると、このxslに従って、xmlが整形されて出力されます。

xsl:output

形式と言語を決めます。

xsl:template match="/"

xmlのどこを起点とするかを設定してます。

xsl:value-of select="books/@title"

xmlの値を出力します。@は属性の場合です。

xsl:for-each

繰り返しです。

xsl:sort

ソートができます。

xsl:element

要素を指定します。

xsl:attribute

属性を指定します。

xsl:choose

分岐です。case文

xsl:when test="price[number(.) &lt;= 2000]

testに結果を返して、条件が成立した場合に次が実行されます。numberは関数で数値化します。

TIPS

リンク

参考書籍

コメント

  • #comment

トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2007-08-20 (月) 10:40:00 (6114d)