- PR -

最後のノードだけ別の処理をしたい

1
投稿者投稿内容
fireworkss
会議室デビュー日: 2007/02/12
投稿数: 11
投稿日時: 2007-02-12 17:39
はじめまして。みなさまご協力お願いします。

最後のノードだけ別のhtmlを使いたいのですが、
どうもうまくいきません。

xmlは
--------------------------------------------------------------------
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="0.xsl"?>
<alll>
<!--ひとつめのall-->
<all>
<data>
<case>ケース1</case>
<text>ああああああああああああああああ</text>
</data>
<data>
<case>ケース2</case>
<text>いいいいいいいいいいいいい</text>
</data>
<data>
<case>ケース3</case>
<text>うううううううううううううう</text>
</data>
<data>
<case>ケース4</case>
<text>ええええええええええええええ</text>
</data>
<data>
<case>ケース5</case>
<text>おおおおおおおおおおおお</text>
</data>
</all>
<!--ふたつめのall-->
<all>
<data>
<case>ケース1</case>
<text>ああああああああああああああああ</text>
</data>
<data>
<case>ケース2</case>
<text>いいいいいいいいいいいいい</text>
</data>
<data>
<case>ケース3</case>
<text>うううううううううううううう</text>
</data>
<data>
<case>ケース4</case>
<text>ええええええええええええええ</text>
</data>
</all>
</alll>
--------------------------------------------------------------------

xslは
--------------------------------------------------------------------
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="alll">
<html>
<head>
<title>XSLサンプル</title></head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="alll/all">
<table border="1" width="500px">
<xsl:for-each select="data[position() mod 2 = 1]">
<tr>
<td width="250px"><xsl:value-of select="case"/></td>
<td width="250px"><xsl:value-of select="following-sibling::Data/case[1]"/></td>
</tr>
<tr>
<td><xsl:value-of select="text"/></td>
<td><xsl:value-of select="following-sibling::Data/text[1]"/></td>
</tr>
<!--all/dataが奇数の場合の最後のノードだけ下記を使用したい-->
<xsl:if test="count(//data) mod 2 = 1">
<xsl:if test="position()=last()">
<tr>
<td colspan="2" width="500px"><xsl:value-of select="case"/></td>
</tr>
<tr>
<td colspan="2"><xsl:value-of select="text"/></td>
</tr>
</xsl:if>
</xsl:if>
<!--all/dataが奇数の場合の最後のノードだけ上記を使用したい-->
</xsl:for-each>
</table>
<br />
</xsl:template>
</xsl:stylesheet>
--------------------------------------------------------------------

です。

長くて申し訳ないですが、
何がしたいかといいますと、
まず、allノードが2つありそれぞれそのなかにdataノードが5つ(奇数)と4つ(偶数)になっていて、
それぞれを2列に表示したかったのです。
それはうまくいったのですが、
5つのほうは当然右側にあきがでます。
なので、”5つのほうだけ(最初のallノード)の最後のノードを別のhtml”という風に
かいたつもりなのですが、表示は両方のallノードの最後の奇数ノードだけ別のhtmlを追加という形になってしまいます。

とてもわかりずらい説明で大変申し訳ないのですが、
どなたかお分かりになる方、いらっしゃいますでしょうか。

まだ、xmlをはじめて間もないので、
具体的にご指摘していただけると幸いです。

よろしくお願いします。


[ メッセージ編集済み 編集者: fireworkss 編集日時 2007-02-12 17:40 ]

[ メッセージ編集済み 編集者: fireworkss 編集日時 2007-02-12 17:40 ]

[ メッセージ編集済み 編集者: fireworkss 編集日時 2007-02-12 17:42 ]

[ メッセージ編集済み 編集者: fireworkss 編集日時 2007-02-12 17:45 ]
だっちょ
大ベテラン
会議室デビュー日: 2006/12/05
投稿数: 115
投稿日時: 2007-02-13 20:20
たぶんやりたいのは次のようなものでしょうが、
もう少しうまい記述がありそうです。

<?xml version='1.0' encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="alll">
<html>
<head>
<title>XSLサンプル</title></head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>

<xsl:template match="alll/all">
<table border="1" width="500px">
<xsl:for-each select="data[position() mod 2 = 1]">
<xsl:choose>
<xsl:when test="(position()=last()) and (position() mod 2 = 1) ">
<tr>
<td colspan="2" width="500px"><xsl:value-of select="case"/></td>
</tr>
<tr>
<td colspan="2"><xsl:value-of select="text"/></td>
</tr>
</xsl:when>

<xsl:otherwise>
<tr>
<td width="250px"><xsl:value-of select="case"/></td>
<td width="250px"><xsl:value-of select="following-sibling:ata/case[1]"/></td>
</tr>
<tr>
<td><xsl:value-of select="text"/></td>
<td><xsl:value-of select="following-sibling:ata/text[1]"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>

</xsl:for-each>
</table>
</xsl:template>


</xsl:stylesheet>
fireworkss
会議室デビュー日: 2007/02/12
投稿数: 11
投稿日時: 2007-02-15 22:04
だっちょさんありがとうございます!!

考え方はわかりましたので、
もっといいやり方というのを検討してみたいと思います!!
1

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