Web Artisan Blog - ウェブ アルチザン ブログ

XML:XSLTで関数(ファンクション)的に処理したい場合:call-template

XML (18 items)

2005年06月22日

XSLT内で同じような処理を複数書く必要性があった場合、
templateを利用して処理を関数的にすることができる。
<例>
・XMLデータ(data12.xml)

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="data12.xsl"?> 
<test>
  <count>3</count> 
  <count>5</count> 
</test>


このXMLデータに対して、
templateを使ってcountノードの値に1プラスした値を出力する。



・XMLデータ(data12.xml)を編集するXSLT(data12.xsl)

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml version="1.0" encoding="Shift_JIS" ?>
<xsl:stylesheet 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output
 method="html" 
 version="1.0" 
 encoding="Shift_JIS" 
 omit-xml-declaration="yes"
 standalone="yes"
 doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
 indent="yes" 
 media-type="text/html"/>

<xsl:template match="/">

  <html>
  <head>
  <title>Result</title>
  </head>
  <body>

  <xsl:apply-templates />

  </body>
  </html>

</xsl:template> 

<xsl:template match="test">
  <xsl:apply-templates select="count" />
</xsl:template> 

<xsl:template match="count">
  count = <xsl:value-of select="." /> <br />
  <xsl:call-template name="countup">
    <xsl:with-param name="prmcount" select="." />
  </xsl:call-template>
</xsl:template> 

<xsl:template name="countup">
<xsl:param name="prmcount"/>
  after call-template = <xsl:value-of select="$prmcount + 1" /><br />
</xsl:template>
</xsl:stylesheet>



call-templateで、カウントアップするtemplate”countup”を呼び出す。


・実行結果のHTML

<html>
<head>
<title>Result</title>
</head>
<body>
count = 3
after call-template = 4
count = 5
after call-template = 6
</body>
</html>




■使用例のダウンロード
ダウンロードし、data12.xmlをブラウザで見る事で確認できます。

>>ダウンロード
※XSLTの動くブラウザでお試し下さい。IEであればIE6以上です。
前の記事 次の記事

Comments

コメントは、まだ書かれていません

Add Comments

Trackback

トラックバックはありません

Trackback URL

http://www.res-system.com/weblog/action.php?action=plugin&name=TrackBack&tb_id=492