XMLをXSLを使いHTMLに変換する方法とエラー対処方法

このQ&Aのポイント
  • XMLをXSLを使用してHTMLに変換する方法と、エラーが発生した場合の対処方法について教えてください。
  • XMLファイルをXSLスタイルシートを使用してウインドウに表示する際にエラーが発生し、表示されない問題が発生しています。エラーの原因や解決策を教えてください。
  • XMLをHTMLに変換するためにXSLスタイルシートを作成しましたが、IE7を使用しても正しく表示されません。正しい変換方法やエラーの修正方法を教えてください。
回答を見る
  • ベストアンサー

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
  • 回答数2
  • ありがとう数5

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

<!-- エラーメッセージを読もう。 Firefoxでは XMLファイルを読み込むとエラーが出た。 Error Parsing XSLT stylesheet,つまりXSLT側に問題があるとわかった。 ・・・ので整形式になってないんだろうな、と予想して XSLTファイルのみを読み込んでエラーメッセージを再確認。 <xsl:template match="Responce/Details"/> 間違えて閉じちゃっている 正しくは・・・ --> <xsl:template match="Responce/Details">

no-ri-ri
質問者

お礼

ありがとうございます。直したところFirefoxでは表示することが出来ました。 ですがIEではXSLは表示されるんですが、XMLは表示することが出来ません。 エラーメッセージを見たのですが、 「ドキュメントの最上位では無効です」 と出てしまいます。 この内容のエラーの解決方法がお分かりでしたらぜひ、教えていただきたいと思いますので、よろしくお願いいたします。

その他の回答 (1)

回答No.2

>ドキュメントの最上位では無効です 結果が異なるはずはないんだけどな。どっちもエラーになるか どっちも成功するか。 まさかとは思うが - <ProductInfo> ↑ここやその他の場所のハイフン実際に入力してたりしないよね? あれはIEが「子要素のある要素」を示す際に利用しているだけだからね。 #文字コードはあっているものとする

no-ri-ri
質問者

お礼

ハイフンは外しています。文字コードも統一しているんですが、 やはりIE7を使用しているのが関係しているんですかね? すぐ返答していただきありがとうございます。

関連するQ&A

  • 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の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
  • 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
  • amazonAPI XSLによるHTML変換エラー

    どなたかご教授頂けたら幸いです。 ※Styleを指定しない場合はxmlが正常に返ってきます。 ※アクセスキーは正確に入力しています。 【エラー内容】 <?xml version="1.0"?> <ItemSearchErrorResponse xmlns="http://ecs.amazonaws.com/doc/2011-08-01/"><Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message></Error><RequestId>607e93a3-0c6e-44ce-a354-c4c2e1c555cd</RequestId></ItemSearchErrorResponse> 【リクエスト】 http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService &AWSAccessKeyId=〇〇〇〇〇〇〇〇〇〇〇〇 &AssociateTag=〇〇〇〇〇〇〇 &Version=2011-08-01 &ResponseGroup=Small &Operation=ItemSearch &SearchIndex=Books &Keywords=質問 &ContentType=text/html &Style=http://www.〇〇〇〇〇.com/sample.xsl (←複数のサーバで試行も不可) ↓ここで署名生成されたSigned URLにてリクエスト http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html 【xsl】 ================================================================================== <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2011-08-01" version="1.0"> <xsl:output method="html" encoding="UTF-8"/> <xsl:template match="/"> <html lang="ja"> <head> <title>テスト</title> </head> <body> <xsl:apply-templates select="aws:ItemSearchResponse/aws:Items/aws:Item"/> </body> </html> </xsl:template> <xsl:template match="aws:ItemSearchResponse/aws:Items/aws:Item"> <p> [タイトル] <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="aws:DetailPageURL" /> </xsl:attribute> <xsl:value-of select="aws:ItemAttributes/aws:Title" /> </xsl:element> [著者] <xsl:value-of select="aws:ItemAttributes/aws:Author" /> </p> </xsl:template> </xsl:stylesheet> ================================================================================== Styleを指定しない場合はxmlが正常に返ってくるのでxslに問題があるのではと思い、 amazonで色々調べましたが、何も発見できませんでした。 宜しくお願いします。

    • 締切済み
    • 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
  • xml+xslでの変換内容をtextareaへ

    お世話になります。 xml+xslでhtmlに変換する際、その変換内容をtextareaにも表示させるようにしたいのです。 使用ブラウザ:FireFox。現状はFirebugでDOMツリーを手動で参照してる状況です。 現在は、ノードをtextareaに表示しようとすると[object DocumentFragment]とだけ出てしまい、DocumentFragmentという概念あることは分かり、色々調べcreateElement()やcreateTextNode()など、その他思いつく限りの事でテストしてみてるのですが、どうにもノードをHTMLタグとしてtextareaに表示させる事が出来ません。 以下にポイントとなるソースを掲示しますので、どなた様か、お助け願えませんでしょうか --test.html---- <html><head> <meta content="text/html; charset=utf8" http-equiv="content-type"> <title>JS+xslt</title> <script type="text/javascript"> function tramsformlists(xmlFile, xslFile, resultNodeID){ var xml, xslt, newDoc; if(document.all){ xml = new ActiveXObject("Microsoft.XMLDOM"); xslt = new ActiveXObject("Microsoft.XMLDOM"); }else { xml = document.implementation.createDocument("", "", null); xslt = document.implementation.createDocument("", "", null); } xml.async = false; xslt.async = false; xml.load(xmlFile); xslt.load(xslFile); if(document.all){ document.getElementById(resultNodeID).innerHTML = xml.transformNode(xslt); }else { var xsltp = new XSLTProcessor(); xsltp.importStylesheet(xslt); newDoc = xsltp.transformToFragment(xml, window.document); var df = document.createDocumentFragment(); cloneDoc= newDoc.cloneNode(true); document.getElementById("Div"+resultNodeID).innerHTML = ""; document.getElementById("Div"+resultNodeID).appendChild(newDoc); t = document.msg_form.TextareaTest.value; document.msg_form.TextareaTest.value =t + df.appendChild(cloneDoc); } } </script> </head><body> <form name="msg_form"> <ul> <li> <input type="button" value="Menu" onclick="tramsformlists('test0.xml','test0.xsl','Test')"><br> <div id="DivTest"></div> <textarea cols="50" rows="10" id="TextareaTest">あああああああ</textarea> </li> </ul> </form> </body></html> --test0.xml-- <?xml version="1.0" encoding="utf-8"?> <root> <nolink_logo> </nolink_logo> </root> --test0.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="html" encoding="utf-8"/> <xsl:template match="root"> <xsl:apply-templates select="nolink_logo"/> </xsl:template> <xsl:template match="nolink_logo"> <xsl:call-template name="Test" /> </xsl:template> <xsl:template name="Test"> <xsl:variable name="menu" select="document('test1.xml')/root"/> <div>画像を表示 <xsl:for-each select="$menu"> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="url[1]"/> </xsl:attribute> <xsl:attribute name="target">_blank</xsl:attribute> <xsl:element name="img"> <xsl:attribute name="src"> <xsl:value-of select="img[1]"/> </xsl:attribute> </xsl:element> </xsl:element> </xsl:for-each> </div> </xsl:template> </xsl:stylesheet> --test1.xml-- <?xml version="1.0" encoding="utf-8"?> <root> <img>http://domain/image2.gif</img> <url>http://domain/test.html</url> </root>

  • htmlに変換されない

    xmlを勉強しています。 しかし、ブラウザでxslファイルを 開いてもhtmlに変換されないまま表示されます。 何が問題なのでしょうか? 両方ともデスクトップに作った同じフォルダの中に 格納しています。 ブラウザは、ie7です。 ------------------------------------------------------ xmlファイル zen.xml <?xml version="1.0" encoding="SHIFT_JIS"?> <?xml-stylesheet type="text/xsl"href="zen.xsl"?> <全体> <情報> <名前>ニコルソン</名前> <住所>アメリカ</住所> </情報> <情報> <名前>フランク</名前> <住所>イタリア</住所> </情報> </全体> -------------------------------------------------- xslファイル zen.xsl <?xml version="1.0" encoding="SHIFT_JIS"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html lang="ja"> <head> <title>顧客情報</title> </head> <body> <div> <xsl:apply-templates select="全体"/> </div> </body> </html> </xsl:template> <xsl:template match="全体"> <xsl:for-each select="情報"> <ul> <li><xsl:value-of select="名前"/></li> <li><xsl:value-of select="住所"/></li> </ul> </xsl:for-each> </xsl:template> </xsl:stylesheet>

    • 締切済み
    • XML
  • XSL中の改行、タブを無効にしたい。

    XMLドキュメントにXSLを適用すると XSL中の改行やタブがそのまま反映されてしまいます。 改行を無くすと1行が長くなってしまい編集が大変で困っています。 どうしたらよいのでしょうか? 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="//z:row"> <xsl:choose> <xsl:when test="(@MACHINE='DELL')"> </xsl:when> <xsl:when test="(@MACHINE='APPLE')"> </xsl:when> <xsl:when test="(@MACHINE='HP')"> </xsl:when> <xsl:otherwise> <xsl:value-of select="@MACHINE" />, <xsl:value-of select="substring(@PARTS_NO,1,1)" />-<xsl:value-of select="substring(@PARTS_NO,2,3)" />-<xsl:value-of select="substring(@PARTS_NO,5,3)" />-<xsl:value-of select="substring(@PARTS_NO,8,2)" />,<xsl:value-of select="@STATUS" />,<xsl:value-of select="@STOCK_NUM" /><xsl:text>&#xA;</xsl:text> <xsl:if test="not(@MACHINE=following-sibling::z:row/@MACHINE)"> <xsl:text>,&#xA;</xsl:text> </xsl:if> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> </xsl:stylesheet>

    • ベストアンサー
    • XML
  • xsl、xpath式の書き方について

    xslで質問させてください。 以下のようなxmlとxslを作っているのですが、xslの最後のほうで、 sample.xmlの/empList/emp/type/@idの値に応じて、typeList.xmlの/typeList/type/titleの値を取得したくて、 xsl:value-of select="document('typeList.xml')/typeList/type[@id = type/@id]/title/text()" /> と書いていますが、うまくいきません。どうすればいいでしょうか? -----[sample.xml] <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="a.xsl" type="text/xsl"?> <empList> <emp> <type id="1" /> <name>鈴木</name> <desc>あああ</desc> </emp> <emp> <type id="2" /> <name>田中</name> <desc>いいい</desc> </emp> </empList> -----[typeList.xml] <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="a.xsl" type="text/xsl"?> <typeList> <type id="1"> <title>タイプ1</title> </type> <type id="2"> <title>タイプ2</title> </type> </typeList> -----[a.xsl] <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="UTF-8" omit-xml-declaration="yes" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" media-type="text/html" /> <xsl:template match="/"> <html> <head><title>サンプル</title></head> <body> <xsl:apply-templates select="empList/emp" /> </body> </html> </xsl:template> <xsl:template match="emp"> <h2><xsl:value-of select="name/text()" /></h2> <p>タイプ:<xsl:value-of select="document('typeList.xml')/typeList/type[@id = type/@id]/title/text()" /></p> <p>メモ:<xsl:value-of select="desc/text()" /></p> </xsl:template> </xsl:stylesheet>

    • ベストアンサー
    • XML
  • XMLをCSVへ変換でほしい情報を取得したい

    XMLからcsvへの変換 下記のXMLのフィールドの項目をxslを使い、msxsl.exe経由で、csvへ変換をしたいのです。 ユーザーデータ部分は抽出可能ですが、aidを抽出する方法がわかりません。 どなたかご教示のほど、お願いいたします。 ■XML <?xml version="1.0" encoding="shift_jis"?> <状況 aid="555"> <ユーザデータ uid="0001"> <氏名>吉野家 太郎</氏名> <ID>123456</ID> <種別>一般</種別> <率>100</率> <最終更新>2012/02/16 15:20:58</最終更新> </ユーザデータ> <ユーザデータ uid="0002"> <氏名>吉野家 次郎</氏名> <ID>123457</ID> <種別>一般</種別> <率>10</率> <最終更新>2012/03/16 15:20:58</最終更新> </ユーザデータ> </状況> ■xsl  ユーザーデータ取得用 <?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="text" encoding="Shift_JIS" /> <xsl:template match="/">uid,氏名,ID,種別,率,最終更新 <xsl:apply-templates select="/状況/ユーザデータ"/> </xsl:template> <xsl:template match="ユーザデータ"> <xsl:value-of select="@uid" /><xsl:text>,</xsl:text> <xsl:value-of select="氏名" /><xsl:text>,</xsl:text> <xsl:value-of select="ID" /><xsl:text>,</xsl:text> <xsl:value-of select="種別" /><xsl:text>,</xsl:text> <xsl:value-of select="率" /><xsl:text>,</xsl:text> <xsl:value-of select="最終更新" /><xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet> aidを取得するにはどのようなXSLを書けばよいのでしょうか? 抽出時のレイアウト aid,uid,氏名,ID,種別,率,最終更新 よろしくお願いいたします。

    • ベストアンサー
    • XML

専門家に質問してみよう