アプリ版:「スタンプのみでお礼する」機能のリリースについて

こんな馬鹿な質問でごめんなさい。XMLファイルをXSLスタイルシートを使って表示させたいですけど、どうやら、XSLファイルの作り方が間違っているようで…
でも、どこだか分からないんです。助けてください。
---XMLファイルの内容は---
<root>
<first>
<second1>22</second1>
<second2>2</second2>
<third1>
<fourth1>4</fourth1>
<fourth2>44</fourth2>
</third>
<third1>
<fourth1>4</fourth1>
<fourth2>44</fourth2>
</third>
</first>
<first>
...

rootの中にはfirstがいくつかあって、
firstの中にはsecondが二つ、thirdがいくつか、
thirdの中にはfourthが二つあります。

xsl:for-each を使って2と4をそのまま表示させたいのですが、

xsl:for-eachの中にxsl:for-eachを書くんですか?
どなたか書き方を教えて下さい。

A 回答 (3件)

マッチしたものを全部出力すればいいだけなら、for-each とか使わなくても、


apply-templates を繰り返していけばいいと思います。例えば次のように。
というか、match したものに対して、その中でいろいろ繰り返すのが for-each
なので、template の中に for-each が出てくるのが基本です。

<?xml version="1.0" encoding="shift_jis"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<html><xsl:apply-templates /></html>
</xsl:template>
<xsl:template match="first">
<xsl:apply-templates/>
!!!!<br/>
</xsl:template>
<xsl:template match="second1">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="second2">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="third1">
<xsl:apply-templates/>
----<br/>
</xsl:template>
<xsl:template match="fourth1">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="fourth2">
<xsl:value-of select="."/><br/>
</xsl:template>
</xsl:stylesheet>
    • good
    • 0
この回答へのお礼

な~んだ☆そうだったんですか!
ありがとうございます(^‐^)
出来ました。馬鹿な質問をしてすみませんでした。

お礼日時:2001/12/20 14:31

どのようなXSLファイルを試されたのでしょうか?



2と4をそのまま表示させたいの意味もあいまいです。22と44は?
具体的なフォーマットは?

second1 と second2 はこのままでは別の名前として扱われますが、
それが意図していることですか?

この回答への補足

ええと、XML文書に少し問題があったようで、Samaraさんが書いている通りです。
second1 とsecond2は別の名前です
表示としては、各firstの終わりのところで、!!!!を出力し、各third1の終わりで----を出力させて、以下のようにしたいです。
22
2
4
44
----
4
44
----
!!!!
。。の繰り返し。
私の書いた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="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<xsl:for-each select="first">
<xsl:apply-templates/>
!!!!<br/>
</xsl:for-each>
</xsl:template>
<xsl:template match="second1">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="second2">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:for-each select="third1">
<xsl:template match="fourth1">
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="fourth2">
<xsl:value-of select="."/><br/>
</xsl:template>
----
</xsl:for-each>
</xsl:stylesheet>
以上です。でも、stylesheet はfor-eachを使えないとエラーが出ました。

補足日時:2001/12/20 13:14
    • good
    • 0

私もXML初心者なのであまり自信はありませんが、書かれているXML自体に問題がありませんか?(それともタイプミスでしょうか?)


以下myrimyriさんが書かれたXMLですが、
---XMLファイルの内容は---
<root>
<first>
<second1>22</second1>
<second2>2</second2>
<third1> ←---------------このタグの終了タグは?
<fourth1>4</fourth1>
<fourth2>44</fourth2>
</third> ←---------------このタグの開始タグは?
<third1> ←-----------?
<fourth1>4</fourth1>
<fourth2>44</fourth2>
</third> ←-----------?
</first>
<first>
...

つまり、<third1>の終了タグの名前が</third>(←終りに" 1 "がない)または<third1>(←はじめに" / "がない)という、状態になっています。どの様なXSLを書かれたのか分かりませんが、とりあえずXMLをもう一度見直してみて下さい。

上のXMLを直すと、こんな感じでしょうか?
<root>
<first>
<second1>22</second1>
<second2>2</second2>
<third1>
<fourth1>4</fourth1>
<fourth2>44</fourth2>
</third1>
<third1>
<fourth1>4</fourth1>
<fourth2>44</fourth2>
</third1>
</first>
<first>
...

お役に立ちますでしょうか?
    • good
    • 0
この回答へのお礼

ごめんなさい。。。タイプミスです。
Samaraさんの直してくれた通りです。
ありがとうございます。

お礼日時:2001/12/20 13:14

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