- まず、Saxon home edition の新しいやつ (saxonhe9-3-0-8j.zip)をダウンロード。
- ダウンロードしたZIPを適当なとこに展開して、中の saxon9he.jar を出しておく。
- Eclipse のメニューを [Window]>[Preference]>[XML]>[XSL]>[Java Processors] ってたどる。
- Add を押して開いたダイアログボックスで、だいたい以下の様な感じで入力。追加したやつのチェックボックスを選択しておく。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/list">
<xsl:variable name="result">
<xsl:call-template name="stat">
<xsl:with-param name="items" select="item" />
<xsl:with-param name="subtotal" select="0" />
<xsl:with-param name="count" select="0" />
</xsl:call-template>
</xsl:variable>
<average>
<xsl:value-of select="$result/avg" />
</average>
<varianece>
<xsl:value-of select="$result/sumOfD2 div $result/count" />
</varianece>
</xsl:template>
<xsl:template name="stat">
<xsl:param name="items"/>
<xsl:param name="subtotal"/>
<xsl:param name="count"/>
<xsl:choose>
<xsl:when test="$items">
<xsl:variable name="t">
<xsl:call-template name="stat">
<xsl:with-param name="items" select="$items[position() > 1]" />
<xsl:with-param name="subtotal" select="$subtotal + $items[1]" />
<xsl:with-param name="count" select="$count + 1" />
</xsl:call-template>
</xsl:variable>
<sumOfD2><xsl:value-of select="($items[1]-$t/avg)*($items[1]-$t/avg)+$t/sumOfD2"/></sumOfD2>
<count><xsl:value-of select="$t/count"/></count>
<avg><xsl:value-of select="$t/avg"/></avg>
</xsl:when>
<xsl:otherwise>
<sumOfD2>0</sumOfD2>
<count><xsl:value-of select="$count"/></count>
<avg><xsl:value-of select="$subtotal div $count"/></avg>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
前回と同じ入力XMLから同じ結果が得られることが確認できた。
前にやった時は、結果ツリーフラグメントの制約から template の結果を文字列にして返さざるを得なくて煩わしかったけど、XSLT 2.0 の temporary tree では 普通の XML データとして扱えるので、やってる事をより直截に表現したコードになった。

0 件のコメント:
コメントを投稿