xslでxmlの1ブロック?を調べるプログラムを教えてください。


たとえば↓のようなxmlがあるとき、
<place>の数をxslで取得するプログラムはどうしたらいいのですか?

また、<place>の子にあたる要素数を取得するxslプログラムを教えてください(おまけ)。

お願いしますm(_ _)m

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="place.xsl"?>
<places>
<place latitude="35.672646" longitude="139.732114" id="0">
<title>ソフトバンククリエイティブ</title>
<description>出版局の所在地です</description>
<link>http://www.sbcr.jp/</link>
<image>sbp.png</image>
</place>
<place latitude="35.671836" longitude="139.734271" id="1">
<title>TBS(東京放送)</title>
<description>東京のキー局のひとつ</description>
<link>http://www.tbs.co.jp</link>
</place>
</places>
-------------------------------------

回答の条件
  • URL必須
  • 1人2回まで
  • 登録:
  • 終了:2006/05/24 19:20:42
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:lains_you No.1

回答回数50ベストアンサー獲得回数10

ポイント35pt

ノード数を取得するcount関数を使用します。


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

<xsl:template match="/">

<xsl:text>placeの数:</xsl:text>

<xsl:value-of select="count(places/place)"/>


<xsl:text>placeの子の数:</xsl:text>

<xsl:value-of select="count(places/place/*)"/>

</xsl:template>

</xsl:stylesheet>


サンプルで覚えるXSLTプログラミング

http://www.atmarkit.co.jp/fxml/tanpatsu/xslt/xslt11.html

id:hiyarihatto

ありがとうございます。

この方法で、出来ました。

今、count(place)の値で分岐するプログラムを作成しているのですが、

<xsl:choose>

<xsl:when test="count(place)>4">

</xsl:when>

<xsl:otherwise>

</xsl:otherwise>

</xsl:choose>

条件によって

が変わるプログラムだと、

うまく動作しません。

テーブルの変わりに文字とかだと大丈夫なんですが‥

どなたか分かる人がいましたら教えてください。

2006/05/24 14:18:07

その他の回答1件)

id:lains_you No.1

回答回数50ベストアンサー獲得回数10ここでベストアンサー

ポイント35pt

ノード数を取得するcount関数を使用します。


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

<xsl:template match="/">

<xsl:text>placeの数:</xsl:text>

<xsl:value-of select="count(places/place)"/>


<xsl:text>placeの子の数:</xsl:text>

<xsl:value-of select="count(places/place/*)"/>

</xsl:template>

</xsl:stylesheet>


サンプルで覚えるXSLTプログラミング

http://www.atmarkit.co.jp/fxml/tanpatsu/xslt/xslt11.html

id:hiyarihatto

ありがとうございます。

この方法で、出来ました。

今、count(place)の値で分岐するプログラムを作成しているのですが、

<xsl:choose>

<xsl:when test="count(place)>4">

</xsl:when>

<xsl:otherwise>

</xsl:otherwise>

</xsl:choose>

条件によって

が変わるプログラムだと、

うまく動作しません。

テーブルの変わりに文字とかだと大丈夫なんですが‥

どなたか分かる人がいましたら教えてください。

2006/05/24 14:18:07
id:lains_you No.2

回答回数50ベストアンサー獲得回数10

ポイント35pt

IE6:End tag 'xsl:when' does not match the start tag 'table'.

Firefox:XML パースエラー: タグの対応が間違っています。終了タグが必要です: </table>


 ブラウザ上で実行した場合、上記のようなメッセージがでませんか?

tableタグを閉じ忘れてます。


 また、正常に分岐しない件ですが、下記の太字のように、count関数内でplaces/を追加

してみてください。きちんとルートからのパスを指定してあげないと、正常にカウントで

きません。


<?xml version="1.0" encoding="Shift_JIS"?>

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

<xsl:template match="/">

<html>

<body>

<xsl:choose>

<xsl:when test="count(places/place) > 4">

<table border="1" CELLSPACING="0" CELLPADDING="0" width="280">

280

</table>

</xsl:when>

<xsl:otherwise>

<table border="1" CELLSPACING="0" CELLPADDING="0" width="300">

300

</table>

</xsl:otherwise>

</xsl:choose>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

id:hiyarihatto

レスありがとうございますm(_ _)m

投稿を見まして、かいけつすることができました。

2006/05/24 19:20:02
  • id:lains_you
     項目の内容はそのまま、テーブルのサイズを変えるだけなら、
    テンプレート化してパラメータを渡すようにするとコードが
    すっきりするとおもいます。
     以下、サンプル。

    &lt;?xml version="1.0" encoding="Shift_JIS"?&gt;
    &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

    &lt;!-- ルートテンプレート --&gt;
    &lt;xsl:template match="/"&gt;
    &lt;html&gt;
    &lt;body&gt;
    &lt;xsl:choose&gt;
    &lt;xsl:when test="count(places/place) &gt; 4"&gt;
    &lt;xsl:call-template name="table"&gt;
    &lt;xsl:with-param name="width"&gt;280&lt;/xsl:with-param&gt;
    &lt;/xsl:call-template&gt;
    &lt;/xsl:when&gt;
    &lt;xsl:otherwise&gt;
    &lt;xsl:call-template name="table"&gt;
    &lt;xsl:with-param name="width"&gt;300&lt;/xsl:with-param&gt;
    &lt;/xsl:call-template&gt;
    &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    &lt;/xsl:template&gt;


    &lt;!-- テーブル用テンプレート --&gt;
    &lt;xsl:template name="table"&gt;
    &lt;xsl:param name="width"/&gt;

    &lt;xsl:value-of select="$width"/&gt;
    &lt;table border="1" CELLSPACING="0" CELLPADDING="0" width="{$width}"&gt;
    &lt;td&gt;あ&lt;/td&gt;
    &lt;/table&gt;
    &lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;
  • id:lains_you
     コメントは実体参照にしなくていいのかorz

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

    <!-- ルートテンプレート -->
    <xsl:template match="/">
    <html>
    <body>
    <xsl:choose>
    <xsl:when test="count(places/place) > 4">
    <xsl:call-template name="table">
    <xsl:with-param name="width">280</xsl:with-param>
    </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
    <xsl:call-template name="table">
    <xsl:with-param name="width">300</xsl:with-param>
    </xsl:call-template>
    </xsl:otherwise>
    </xsl:choose>
    </body>
    </html>
    </xsl:template>


    <!-- テーブル用テンプレート -->
    <xsl:template name="table">
    <xsl:param name="width"/>

    <xsl:value-of select="$width"/>
    <table border="1" CELLSPACING="0" CELLPADDING="0" width="{$width}">
    <td>あ</td>
    </table>
    </xsl:template>
    </xsl:stylesheet>

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません