重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

【GOLF me!】初月無料お試し

以下のようなXMLファイルを作成した(XML宣言省略)

<purchase-order>
<book>
<title>古い算数</title> <!--注文する本の題名-->
<price>1500</price> <!--一冊あたりの本の値段-->
<count>6</count> <!--注文した冊数-->
</book>
<book>
<title>楽しくない国語</title>
<price>1000</price>
<count>4</count>
</book>
<book>
<title>詳解リカバリ</title>
<price>4000</price>
<count>5</count>
</book>
</purchase-order>

データ中の本のタイトルは気にしないでください(^^;;;
この注文全体について一冊あたりの平均価格(つまり(1500*6+1000*4+4000*5)/(6+4+5))を求めたいのですが、
どうやって求めたらいいのでしょうか?
6+4+5の部分だけなら

前略
<xsl:template match="purchase-order">
<xsl:value-of select="sum(count)">
<xsl:template>
後略

でうまくいくんですが(別なデータでは上手くいった。関数名との競合は起きないと思うんですが..)
分子を出すにあたって、selectに指定する内容が解らないんです。

A 回答 (2件)

初書き込みです。


時間がったているので解決したかと思いますが、レスしますね。
以下のテンプレートを使用すれば、出来るのかなと思います。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="purchase-order" />
</body>
</html>
</xsl:template>

<xsl:template match="purchase-order">
<xsl:variable name="total_sum">
<xsl:call-template name="culc_sum">
<xsl:with-param name="tanka" select="book/price" />
<xsl:with-param name="suryou" select="book/count" />
</xsl:call-template>
</xsl:variable>
<xsl:text>合 計</xsl:text>
<xsl:value-of select="format-number( $total_sum div sum(book/count),'#,###')" />
</xsl:template>

<xsl:template name="culc_sum">
<xsl:param name="tanka" />
<xsl:param name="suryou" />
<xsl:choose>
<xsl:when test="not($tanka) or not($suryou)">
<xsl:value-of select="0" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="sum_data">
<xsl:call-template name="culc_sum">
<xsl:with-param name="tanka" select="$tanka[position()>1]" />
<xsl:with-param name="suryou" select="$suryou[position()>1]" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="number($tanka[1]) * number($suryou[1]) + $sum_data" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
    • good
    • 0
この回答へのお礼

実は解決しておりませんでした(汗)
ご回答ありがとうございます。
(僕が未熟なだけかもしれないけど)ソース見てこんな書き方もあるんだなぁと感心しました

お礼日時:2005/06/22 23:16

訂正です。


<xsl:text>合 計</xsl:text>
ではなくて、
<xsl:text>平均価格</xsl:text>
ですね。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!