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

XML:XSLTで最大値を求める

XML (18 items)

2005年03月07日

Research Artisan Pro ←リサーチアルチザンがパワーアップして復活しました!!
XSLT関数には、「count関数」や「sum関数」のような数値計算関数があるが、
「max関数」といったように関数として最大値を求めれるものはない。
しかし、「sort」や「position関数」を使用して最大値を求める事は可能である。
<例>
・XMLデータ(data10.xml)

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



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

<?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出力部 -->
  <html>
  <head>
  <title>Result</title>
  </head>
  <body>

  <xsl:apply-templates />

<!-- HTML出力部 -->
  </body>
  </html>

</xsl:template> 

<xsl:template match="test">
  <xsl:apply-templates select="count">
    <!-- 降順でソート -->
    <xsl:sort select="." data-type="number" order="descending" />
  </xsl:apply-templates>
</xsl:template> 

<xsl:template match="count">
    <!-- 降順でソートした結果の最初のデータ=最大値 -->
    <xsl:if test="position() = 1">
      <xsl:value-of select="." />
    </xsl:if>
</xsl:template> 

</xsl:stylesheet>


※countノードで降順ソートし、結果の一番最初のデータが最大値になる考えを利用。


・実行結果のHTML

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




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

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

Comments

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

Add Comments

Trackback

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

Trackback URL

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