たとえば↓のような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>
-------------------------------------
ノード数を取得する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プログラミング
ありがとうございます。
この方法で、出来ました。
今、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件)
lains_you
50
10
ここでベストアンサー
ノード数を取得する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
ありがとうございます。
この方法で、出来ました。
今、count(place)の値で分岐するプログラムを作成しているのですが、
<xsl:choose>
<xsl:when test="count(place)>4">
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
条件によって
うまく動作しません。
テーブルの変わりに文字とかだと大丈夫なんですが‥
どなたか分かる人がいましたら教えてください。
2006/05/24 14:18:07lains_you
50
10
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>
レスありがとうございますm(_ _)m
投稿を見まして、かいけつすることができました。
コメント(2件)
テンプレート化してパラメータを渡すようにするとコードが
すっきりするとおもいます。
以下、サンプル。
<?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>
<?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>
この質問への反応(ブックマークコメント)
これ以上回答リクエストを送信することはできません。制限について