http://www.sw.it.aoyama.ac.jp/2009/PB2/lecture12.html
© 2007-10 Martin J. Dürst 青山学院大学
1 月18日 (月曜日) 3時限に最後の授業
1 月 25日 (月曜日) 3時限 (13:10-14:10)
過去の問題:
本ごとの郵送料を計算する。注文のリストにそのための新しい欄を作る。本の全体の値段に足す。(詳細略)
ヒント (Test-Driven Development):
正解例: ordersShipping.xsl,
その出力: ordersShipping.html
<xsl:apply-templates>
で自然に繰り返す<xsl:for-each>
で明記的な繰り返しが必要本をテーブルとして出力する変換ファイルを次のように変更する:
<xsl:attribute>
を使用する正解例: tableStripes.xsl,
その出力: resultStripes.html
正解例: tableClump.xsl (出力は
<xsl:apply-templates>
を使ったものと変わらない)
問題:<xsl:template match='book[(position() mod 3) =
0]'>
で分岐しようとすると、ソートがうまくできない
実例: tableStripesTemplate.xsl,
その出力: tableStripesTemplate.html
理由:
<xsl:template> の @match 内は XPath
ではなく、パターンposition()
などは元の文章内の位置で算出される解決:
<xsl:apply-templates> でも
<xsl:for-each> でも結構@match
ではなく、<xsl:choose> や
<xsl:if><xsl:apply-templates> などが使えない)<xsl:template> 見たいなものを使いたい)<xsl:template>
要素を使う@match の代わりに @name を使う<xsl:template name='greeting'> <p>Hello <strong>dear customer</strong>!</p> </xsl:template>
<xsl:call-template> 要素を使う@name で呼びたいテンプレートを指定<xsl:template> の中:
出力の一部を指定<xsl:variable> の中:
変数の値の一部を指定<xsl:template ...> ... <xsl:call-template name='greeting'/> ... </xsl:template>
parameter (ひきすう)
<xsl:call-template> の中に
<xsl:with-param> 要素を使う<xsl:template> の中 (一番最初) に
<xsl:param> を使う<xsl:with-param> 要素で @name
で引数の名前、@select
又は要素の内容で値を指定<xsl:param> 要素で@name
で引数の名前、必要であれば @select
又は要素の内容で即定値を指定<xsl:template name='greeting'> <xsl:param name='who' select='"customer"'/> <p>Hello <strong>dear <xsl:value-of select='$who'/></strong>!</p> </xsl:template> ... <xsl:call-template name='greeting'> <xsl:with-param name='who'>青山 例子</xsl:with-param> </xsl:call-template>
<xsl:template name='階乗'>
<xsl:param name='n'/>
<xsl:choose>
<xsl:when test='$n = 1'>1</xsl:when>
<xsl:otherwise>
<xsl:variable name='n1'>
<xsl:call-template name='階乗'>
<xsl:with-param name='n' select='$n - 1'/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select='$n * $n1'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<div> の中の <div>
で、<xsl:template match='div'>
が再帰的に使用<xsl:param> のもう一つの使い方<xsl:transform> (又は
<xsl:stylesheet>) 直下で
<xsl:param> を使うmsxsl の場合:Z:\>msxsl data.xml transform.xsl name=value >result.htmlヒント:
<xsl:template>
が必要正解例: fac.xsl
提出: 1月17日 (日曜日) 22:00 までに Moodle に投稿
(例えば新年とか成人式の) 挨拶文を (X)HTML で出力する変換ファイルを作る。
相手の名前と挨拶文の回数は外側から指定できるようにする。
ヒント: 「n 回の挨拶文」を階乗と同じように定義しなおす。
授業全体の内容を復習し、分からない部分をリストアップ