• 締切済み

XML/XSLでテンプレートの適用がうまくいかない

同じツリーの深さで異なる要素名のノードがあるXMLだと期待通りにテンプレートが適用できない という問題に付き当たっています。 例えば    / +------+ poem  poem のような構成だとmatch=poem、value-of select="text"などで各poemの歌詞を表示させることが できます。 一方、以下の構成で同じ事をやろうとすると各poemの歌詞に加えてyearのnumのValueまで 列挙されてしまいます。poemの中身だけ出すにはどうすればよいでしょうか? 以下に例を書きます。     / +--------------+ list         year +------+      +----+ poem poem    Num Num 例えば以下のURLなどを参照してやっております。 http://www6.airnet.ne.jp/manyo/xml/xslt/step15.html XSLはこんな感じです。 <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="poem"> <tr> <td><xsl:value-of select="poet" /></td> <td><xsl:value-of select="yomi" /></td> </tr> <br /> </xsl:template> 手持ちのXMLを変換したく、その勉強のため上記のような構成のXMLを扱う方法を 知りたいと思っています。 よろしくお願いします。

  • XML
  • 回答数1
  • ありがとう数0

みんなの回答

  • dscripty
  • ベストアンサー率51% (166/325)
回答No.1

> 以下に例を書きます。 Tree 表記の代わりに、実際の XML を書いてみるといいよ。 あと、どのように変換されて欲しいのか分かるように、期待する変換後の結果も必要かな?

関連するQ&A

  • XSLのifがいまいち理解ができなく。

    if文をどうやればいいのかで悩んでしまいに詰まっている状態です。 URLがない場合は何も表示しないにしたいのですが、参考ページを見ても全くわからない状態。 test.xsl <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet> <xsl:template match="/"> <xsl:apply-templates select="page/message"/> </xsl:template> <xsl:template match="page/message"> <table> <tr> <td>名前</td> <td><xsl:value-of select="name"/></td> </tr> <tr> <td>URL</td> <td><a><xsl:attribute name="href"><xsl:value-of select="@url" /></xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:value-of select="@url" /></a></td> </tr> </table> </xsl:template> </xsl:stylesheet> xml <?xml version="1.0" encoding="utf-8"?> <page> <message url="http://yahoo.co.jp"><name>山田太郎</name></message> <message url=""><name>山田花子</name></message> </page>

    • ベストアンサー
    • XML
  • XSL内でJavaScriptを記述し、JavaScript内でXMLデータを取得するには・・・

    大変困っていますのでよろしくお願いします。 XSLのファイル内でJavaScriptを記述し、JavaScriptでxmlの要素を取得操作したいのですが、とり方がわかりません。 ご教授のほどをよろしくお願いします。 ================================================================== sample.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="/"> <html> <head> <title>XML: 基本サンプル</title> </head> <body> <p align="center">サンプル</p> <xsl:apply-templates select="information/value/applicationinfo" /> </body> </html> </xsl:template> <xsl:template match="value/applicationinfo"> <Script Language = "JavaScript"> <xsl:comment> <![CDATA[ document.write("<xsl:value-of select=\"componentname\" />"); ]]> </xsl:comment> </Script> <table border="1"> <tr> <td><xsl:value-of select="componentname" /></td> </tr> </table> </xsl:template> </xsl:stylesheet> ================================================================== 下のテーブルでは要素を表示することはできます。 このやり方では駄目なのでしょうか?

    • ベストアンサー
    • XML
  • xsl:variable の使用方法

    こんにちは xml,xsl を今日はじめて使う初心者です。 変数を使おうとして下記のようなメッセージが出てしまいます。 >ここではキーワード xsl:variable を使用できません。 どうすればよろしいでしょう? area.xls: <?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> (中略) </html> </xsl:template> <xsl:template match="変換結果"> <table align="left" border="1" width="1500" cellspacing="0" cellpadding="0"> <tr> (中略) </tr> <tr> (中略) <xsl:comment>合計 </xsl:comment> <th>数量</th> (中略) </tr> <xsl:variable name="coLor" select="white" /> <xsl:for-each select="row" order-by="+column0"> <tr bgcolor="$coLor"> <td width="200" align="right"> <xsl:value-of select="column0" /> </td> (中略) </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> ************************************************************* area.xml <?xml version="1.0" encoding="Shift_Jis"?> <?xml-stylesheet type="text/xsl" href="area.xsl" ?> <変換結果> <row row-Num='1'><column0>,4536163880211</column0> (中略) </row> </変換結果>

    • ベストアンサー
    • XML
  • xmlから吐き出したhtmlデータのソースインデントがきかない

    xmlとxsltを使って、htmlを吐き出しているのですが、 <xsl:output indent="yes"/> indent="yes"を入れてもindentされません。 xsltは下記になります。 <?xml version="1.0" encoding="shift_jis"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="id" /> <xsl:output method="xml" standalone="yes" encoding="Shift_JIS" omit-xml-declaration="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" indent="yes" /> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>KDS</title> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis" /> <link rel="stylesheet" href="/css/style.css" type="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <script type="text/javascript" src="/js/snavi_case.js"><xsl:comment>//</xsl:comment></script> </head> <body> <xsl:apply-templates select="info/group/page[@fileId='pteach' and @fileId = $id]" mode="main"/> </body> </html> </xsl:template> <xsl:template match="info/group/page" mode="main"> <xsl:value-of select="title"/> <table width="539" border="1"> <xsl:apply-templates select="teacher[position() mod 3 = 1]"/> </table> </xsl:template> <xsl:template match="teacher"> <tr> <td><xsl:value-of select="name"/>:<xsl:value-of select="subject"/></td> <td><xsl:value-of select="following-sibling::teacher[1]/name"/>:<xsl:value-of select="following-sibling::teacher[1]/subject"/></td> <td><xsl:value-of select="following-sibling::teacher[2]/name"/>:<xsl:value-of select="following-sibling::teacher[2]/subject"/></td> </tr> </xsl:template> </xsl:stylesheet> ただ、<html>~<body>と</body>~</html>はしっかりとソースがインデントされるのですが、<xsl:apply-templates select="info/group/page[@fileId='pteach' and @fileId = $id]" mode="main"/>で読み込んだところ以降のindentが全くきいていないようです。 こんな感じになります。 <html> <title></title> <body>#ここまではindentは正常 <table><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr><tr><td></td></tr></table> #indentが効いていない! </body> </html> どなたか同じような状況になられた方、 いらっしゃいませんでしょうか。

    • ベストアンサー
    • XML
  • XMLをXSLを使いHTMLに変換したいのですが・・・・

    あるXML形式のXSL用に次のようにスタイルシートを作成致しました。(例) <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" version="4.01" encoding="UTF-8" indent="yes" /> <xsl:template match="/"> <html lang="ja"> <head> <title>○○情報</title> </head> <body><xsl:apply-templates select="ProductInfo"/></body> </html> </xsl:template> <xsl:template match="Responce/Details"/> <h2><xsl:value-of select="Hid"/></h2> <h2><xsl:value-of select="TotalItems"/></h2> <h2><xsl:value-of select="TotalPages"/></h2> <h3><xsl:value-of select="ItemID"/></h3> <h3><xsl:value-of select="ShopID"/></h3> <h3><xsl:value-of select="ProductName"/></h3> <h3><xsl:value-of select="Price"/></h3> <h3><xsl:value-of select="ImageUrlSmall"/></h3> <h3><xsl:value-of select="ImageUrlMedium"/></h3> <h3><xsl:value-of select="mageUrlLarge"/></h3> <h3><xsl:value-of select="URL"/></h3> <h3><xsl:value-of select="MobileURL"/></h3> <h3><xsl:value-of select="Manufacture"/></h3> <h3><xsl:value-of select="Availability"/></h3> <h3><xsl:value-of select="Stock"/></h3> <h3><xsl:value-of select="StockInfinity"/></h3> <h3><xsl:value-of select="ItemDetail"/></h3> <h3><xsl:value-of select="SalesStatus"/></h3> <h3><xsl:value-of select="CategoryID"/></h3> </xsl:template> </xsl:stylesheet> とスタイルシートを作成いたしました。 変換するHMLは <?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/xsl" href="○○.xsl"?> - <ProductInfo> - <Responce> <Hid>○○○○</Hid> <TotalItems>○○○○</TotalItems> <TotalPages>○○○○</TotalPages> - <Details> <ItemID>○○○○</ItemID> <ShopID>○○○○</ShopID> <ProductName>○○○○</ProductName> <Price>○○○○</Price> <Manufacture>○○○○</Manufacture> <Availability>○○○○</Availability> <Stock>0○○○○</Stock> <StockInfinity>○○○○</StockInfinity> <ItemDetail>○○○○</ItemDetail> <SalesStatus>○○○○</SalesStatus> <CategoryID>○○○○</CategoryID> </Details> </Responce> </ProductInfo> 以下<Details>のみ複数繰り返しされます。 この通りに作成したのですが、XMLファイルをウインドウに表示しようとしてもエラーが報告されます。 XSLに繰り返し用のタグを入れても、まったく表示されません。 ちなみにIE7を使用しています。 もし間違っている箇所が分かる方がいらっしゃいましたら、教えていただけませんか? よろしくお願いいたします。

    • ベストアンサー
    • XML
  • xslのdocument関数で読込んだ2つのxmlをxsl:sortしたい

    次のXSLで動作させています。 <?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet xml:space="default" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"> <xsl:output method="text" /> <xsl:template match="/"> <xsl:for-each select="document('取り込むXMLファイル')//z:row"> <xsl:value-of select="@取り出したい属性の名前" /> </xsl:for-each> <xsl:for-each select="//z:row"> <xsl:value-of select="@取り出したい属性の名前" /> </xsl:for-each> </xsl:template> </xsl:stylesheet> ここまでは出来たのですが、xsl:sortでまとめてsortするにはどうすればいいのでしょうか?

    • ベストアンサー
    • XML
  • XSLの中でHTMLタグが使えない

    XMLとXSLTを使って、Formの中のListのオプションを、XML から取ってきたいと思ってます。イメージ的には、下記 の○○○の中に、<xsl:value-of select="."/>を入れる ことによって、<option value="<xsl:value-of select="."/>"><xsl:value-of select="."/></option> の様にしたいのですが、<や>を&lt;や&gt;に変えても、 うまく表示されません。また、Selectタグの属性の Multipleも=サインが無いとかで使えませし,Inputタグ も、終了タグが無いため使えませんでした。どのように 対処したら良いのでしょうか?よろしくお願いしま す。 <select name="list1" size="10" id="select5"> <xsl:apply-templates/> </select> <xsl:template match="//name"> <option value="○○○">○○○</option> </xsl:template>

    • 締切済み
    • XML
  • XSLで、XMLの空タグを制御したい。

    値がないXML文書に、 <exsample /> こう書かれてしまうものを、 <exsample></exsample> このように出力したいのですが、出来ずでした。 以下は、私のやり方です。 ■ XSL <?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt"> <xsl:output method="xml" encoding="Shift_JIS" indent="yes" xalan:indent-amount="4"/> <xsl:template match="/"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> ■ XML <?xml version="1.0" encoding="Shift_JIS"?> <root> <section> <category> <exsample1>value</exsample1> <exsample /> </category> </section> </root> ご教授、よろしくお願いいたします。

    • ベストアンサー
    • XML
  • XMLをXSLTやCSSに変換してHTMLで出力

    XMLデータ <?xml version="1.0" encoding="Shift-JIS"?> <flower> <naiyou> 本文 </naiyou> <name>花言葉</name> <flow><name>あきらめ </name></flow> <flow><list>悲しい思い出 </list></flow> <flow><name>情熱 </name></flow> <flow><list>独立 </list></flow> <flow><name>再会 </name></flow> <flow><list>想うはあなた一人 </list></flow> <flow><name>また会う日を楽しみに </name> </flow> </flower> を変換したいのです。 XSLTデータ <?xml version="1.0" encoding="Shift-JIS"?> <xsl:stylesheet version="1.0" xmlns:xsl="F:\i08i008\I08I008.html"> <xsl:output method="html"/> <xsl:template match="/"> <html><xsl:apply-templates/></html> </xsl:template> <xsl:template match="flower"> <h1>彼岸花</h1>  <xsl:apply-templates/>  </xsl:template> <xsl:template match="naiyou"> <p><xsl:value-of select="."/></p> </xsl:template> <xsl:template match="flow"> <xsl:value-of select="name"/> <table flame="border" width="50%"> <tr><th>彼岸花</th><th>花言葉</th></tr> <xsl:for-each select="flow"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="list"/></td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> ここまで作ったのですが、うまくHTMLに出力できません。原因がわかる方、教えてください。 イメージ図 <html> <head> <title>彼岸花</title> </head> <body> <div style="font-weight:bold"> 彼岸花 </div> ヒガンバナ科ヒガンバナ属の多年草。別名「曼珠沙華」という別名があるのは有名だが、他にも呼び名があるのは知られていない。<br> ただ、その名前が「地獄花」、「捨子花」などと縁起でもない名前ばかりである。<br> その中でも何故「はっかけばばあ」という呼び名があるのかは疑問である。<br> <img src="http://farm1.static.flickr.com/120/252830085_712ffd338b.jpg"width="171" height="171"><br> <br> <table border="1"bgcolor="#b22222"> <tr> <th colspan="2">彼岸花</th> </tr> <tr> <th colspan="2">花言葉</th> </tr> <tr> <th>悲しい思い出</th> <th>独立</th> </tr> <tr> <td>独立</td> <td>諦め</td> </tr> <tr> <td>再開</td> <td>情熱</td> </tr> </table> </body> </html> XSLTについては(CSSもそうですが)、イメージ図の通り、写真を掲載したりや表に配色をしたいとも考えております。そのあたりのアドバイスも頂けると嬉しいです。 あとCSSについてもサイトで色々見たのですが、どれもさっぱり解かりませんでした。初心者な私にでも解かりやすいサイト教えてください。一から勉強しているところです。 CSSはこういう風に記述するんだよ、と簡単に教えて頂ける優しい方もいたら回答お願いします。サイトだけでは悪戦苦闘しそうなもので…。物凄い不安です。

    • 締切済み
    • XML
  • XMLの実体参照とXSLT

    次のようなXMLファイルをXSLTで処理したいのですが、&baseの部分がうまく表示できません。 どのようにしたら表示できるでしょうか? ■a.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="a.xsl"?> <!DOCTYPE test [ <!ELEMENT name (#PCDATA)> <!ENTITY base SYSTEM "base.xml"> ]> <test> <name>abc</name> &base; </test> ■base.xml <?xml version="1.0" encoding="UTF-8"?> <base>base</base> ■a.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8" /> <xsl:template match="/"> <xsl:apply-templates select="test" /> <xsl:apply-templates select="base" /> </xsl:template> <xsl:template match="test"> name:<xsl:value-of select="name" /> </xsl:template> <xsl:template match="base"> base:<xsl:value-of select="base" /> </xsl:template> </xsl:stylesheet> □結果 name:abc ※base.xmlの内容を表示できない

    • ベストアンサー
    • XML

専門家に質問してみよう