- PR -

XSLTでの加算処理

1
投稿者投稿内容
すみれ
会議室デビュー日: 2005/07/12
投稿数: 13
投稿日時: 2006-08-09 18:24
いつも大変お世話になっております。

XSLTで以下のようにHTMLへ変換しようとしているのですが、処理方法がわからなくなってしまって書き込みました。

HTMLの各列の幅に<width>属性を作成し、そこにXMLの<tgroup>要素<widths>属性から取った値のうち、<colname>属性番目に値するものを、表示させています。
'in 'で区切って処理する方法は、下記投稿を参考にさせていただきました。
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=16378&forum=9

しかしHTMLで表示させたときに幅が広がってしまったので、HTMLの<table>タグの<width>属性に各列の幅の総和を表示しようと思っています。
そこで混乱してしまっています。
各幅に表示させた<width>属性の値の総和をどうやって、<table>の<width>属性へ渡したらよいのでしょうか。
どなたかお力を貸していただけませんでしょうか。
よろしくお願いいたします。


□XML
<tgroup cols = "6" widths = "0.685in 0.979in 1.106in 1.106in 1.106in 1.341in">
<thead>
<row>
<entry colname = "1">aaa</entry>
<entry colname = "2">bbb</entry>
<entry colname = "3">ccc</entry>
<entry colname = "4">ddd</entry>
<entry colname = "5">eee</entry>
<entry colname = "6">fff</entry>
</row>
</thead>
</tgroup>

□HTML
<table width=" "><!--table要素にwidth属性を作成し、属性値に各幅の総和を表示させたい-->
<thead>
<tr>
<th width="49">aaa</th>
<th width="70">bbb</th>
<th width="79">ccc</th>
<th width="79">ddd</th>
<th width="79">eee</th>
<th width="96">fff</th>
</tr>
</thead>
</table>

□XSLT
<xsl:template match="tgroup">
<table>
<thead>
<tr>
<xsl:for-each select="thead/row/entry">
<th>
<xsl:call-template name="widthtemplate"/>
</th>
</xsl:for-each>
</tr>
</thead>
</table>
</xsl:template>

<xsl:template name="widthtemplate">
<xsl:attribute name="width">
<xsl:call-template name="takeout">
<xsl:with-param name="str" select="ancestor::tgroup/@widths"/>
<xsl:with-param name="num" select="@colname"/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>

<xsl:template name="takeout">
<xsl:param name="str"/>
<xsl:param name="num"/>
<xsl:variable name="kugiri" select="'in'"/>
<xsl:variable name="num1" select="$num - 1"/>
<xsl:variable name="car" select="substring-before($str, $kugiri)"/>
<xsl:variable name="cdr" select="substring-after($str, $kugiri)"/>
<xsl:choose>
<xsl:when test="$num1 = 0">
<xsl:value-of select="floor($car *72)"/><!--実数計算後、表示-->
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="takeout">
<xsl:with-param name="str" select="$cdr"/>
<xsl:with-param name="num" select="$num1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

[ メッセージ編集済み 編集者: すみれ 編集日時 2006-08-09 19:25 ]
未記入
常連さん
会議室デビュー日: 2005/07/04
投稿数: 31
投稿日時: 2006-08-09 19:24
コード:
  <xsl:template match="tgroup">
    <table>
      <xsl:attribute name="width">
	<xsl:call-template name="sumup">
          <xsl:with-param name="num" select="1"/>
          <xsl:with-param name="sum" select="0"/>
	</xsl:call-template>
      </xsl:attribute>
    </table> 
  </xsl:template> 

  <xsl:template name="sumup">
    <xsl:param name="num"/>
    <xsl:param name="sum"/>
    <xsl:variable name="w">
      <xsl:call-template name="takeout"> 
	<xsl:with-param name="str" select="/tgroup/@widths"/> 
	<xsl:with-param name="num" select="$num"/> 
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$num = count(/tgroup/thead/row/entry)">
        <xsl:value-of select="$sum + $w"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="sumup">
          <xsl:with-param name="num" select="$num + 1"/>
          <xsl:with-param name="sum" select="$sum + $w"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="takeout"> 
    <xsl:param name="str"/> 
    <xsl:param name="num"/> 
    <xsl:variable name="kugiri" select="'in'"/> 
    <xsl:variable name="num1" select="$num - 1"/> 
    <xsl:variable name="car" select="substring-before($str, $kugiri)"/> 
    <xsl:variable name="cdr" select="substring-after($str, $kugiri)"/> 
    <xsl:choose> 
      <xsl:when test="$num1 = 0"> 
	<xsl:value-of select="floor($car *72)"/><!--実数計算後、表示--> 
      </xsl:when> 
      <xsl:otherwise> 
	<xsl:call-template name="takeout"> 
	  <xsl:with-param name="str" select="$cdr"/> 
	  <xsl:with-param name="num" select="$num1"/> 
	</xsl:call-template> 
      </xsl:otherwise> 
    </xsl:choose> 
  </xsl:template>



もっと簡単なやり方あるのかな?
MMX
ぬし
会議室デビュー日: 2001/10/26
投稿数: 861
投稿日時: 2006-08-10 12:37
その他の方法
OFFICEのWeb形式で保存のhtmlのように、
width を指定するための、空でhiddenなrowを 末尾に立て
その中に td を並べる。
幅の取り出しは 再帰になるのは、同じです

[ メッセージ編集済み 編集者: MMX 編集日時 2006-08-10 13:04 ]
すみれ
会議室デビュー日: 2005/07/12
投稿数: 13
投稿日時: 2006-08-10 16:23
未記入さん、MMXさん、ご返答ありがとうございます。

再帰処理と変数処理が混ざって整理がつかずにいました。
現状として、XSLTの再帰処理が適していると思われるので、未記入さんの方法を参考にさせていただき、目的の値をとることができました。
MMXさんの方法、今後の参考にさせていただきます。

重ね重ね、ありがとうございました。
1

スキルアップ/キャリアアップ(JOB@IT)