XMLからHTMLに変換&ページ分割したい

このQ&Aのポイント
  • XMLからHTMLに変換し、ページ分割を行いたい。
  • 1000個のデータがある場合でも、ページ分割をして見やすいインデックスページを作成したい。
  • 現在のXSLでは3ページしか出力されないため、10個までのデータを1ページに出力し、それ以上のデータは次のページに出力したい。
回答を見る
  • ベストアンサー

xml→htmlへの変換&ページ分割したい

まず、下記のxml、xslによりhtmlページを出力しています。 ■test.xml <?xml version="1.0" encoding="UTF-8"?> <all> <index file_id="a"> <product file_id="a-01"> <yoso>ああああああああああああああああ</yoso> </product> <product file_id="a-02"> <yoso>いいいいいいいいいいいいいいいい</yoso> </product> <product file_id="a-03"> <yoso>うううううううううううううううう</yoso> </product> <product file_id="a-04"> <yoso>ああああああああああああああああ</yoso> </product> <product file_id="a-05"> <yoso>いいいいいいいいいいいいいいいい</yoso> </product> <product file_id="a-06"> <yoso>うううううううううううううううう</yoso> </product> </index> <index file_id="b"> <product file_id="b-01"> <yoso>ああああああああああああああああ</yoso> </product> <product file_id="b-02"> <yoso>いいいいいいいいいいいいいいいい</yoso> </product> <product file_id="b-03"> <yoso>うううううううううううううううう</yoso> </product> <product file_id="b-04"> <yoso>うううううううううううううううう</yoso> </product> </index> <index file_id="c"> <product file_id="c-01"> <yoso>ああああああああああああああああ</yoso> </product> <product file_id="c-02"> <yoso>いいいいいいいいいいいいいいいい</yoso> </product> <product file_id="c-03"> <yoso>うううううううううううううううう</yoso> </product> </index> </all> ■ind.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0" xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect" extension-element-prefixes="redirect"> <xsl:output method="html" encoding="UTF-8"/> <xsl:template match="index"> <!-- 出力ファイル名 --> <xsl:variable name="filename" select="concat('html/',@file_id, '/index.html')"/> <!-- 出力開始 --> <redirect:open select="$filename"/> <redirect:write select="$filename"> <html> <head> <title><xsl:value-of select="@file_id"/></title> </head> <body> <h1><xsl:value-of select="@file_id"/></h1> <xsl:apply-templates /> </body> </html> </redirect:write> <redirect:close select="$filename"/> <!-- 変換報告 --> <xsl:value-of select="concat('『',$filename,'』変換完了')"/> </xsl:template> <xsl:template match="product"> <a> <xsl:attribute name="href"> <xsl:value-of select="concat(@file_id,'.html')" /> </xsl:attribute> <xsl:value-of select="@file_id" /> </a><br /> </xsl:template> </xsl:stylesheet> ■環境 ・xalan-j_2_7_1 ・j2sdk1.4.2_16 ・windowsxp コマンドプロンプトにより下記を実行すると java org.apache.xalan.xslt.Process -in test.xml -xsl ind.xsl html/a/index.html html/b/index.html html/c/index.html が出力されます。 つまりは各カテゴリ(a,b,c)のproduct要素をまとめた インデックスページが出力されます。 ここまでが現状です。 やりたいのは以下です。 上記だとproductの数にかかわらず1ページのindex.htmlに 出力されます。 test.xmlは数が少ないのでいいですが、 仮に1000個あった場合、どんでもないことになってしまうので、 ページ分割をしたいと思っています。(< 1 2 3 4 > みたいな!) ですが、何をどうしたらよいのか皆目見当が付きません。 1ページを10個までとしてそれ以上は次ページに出力みたいなことはできるのでしょうか。 html/a/index.html html/a/index2.html html/a/index3.htmlみたいにです。。。 上記のxslだとmatchするtemplates(index)は3つだけなので、 xalanが書き出すのは3ぺーじだけです。 この書き出す回数をたとえばcount(product div 10)とかにできれば、 なにかみえてきそうなきもするんですが、、、、 長々と申し訳ありませんが、 ご協力お願いします。

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

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

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

JDK 7でやろうとしたらエラーが出たし、エラーの原因を把握して動作するようにするのも面倒なのでメモだけ <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0"> <xsl:output method="html" encoding="UTF-8"/> <xsl:template match="/all"> <html> <head> <title>Q3861990 TestCase 1</title> </head> <body> <dl> <xsl:apply-templates select="index"/> </dl> </body> </html> </xsl:template> <xsl:template match="index"> <xsl:apply-templates select="product" /> </xsl:template> <xsl:template match="product"> <xsl:variable name="pos" select="floor((position() - 1) div 3)" /> <xsl:call-template name="compare"> <xsl:with-param name="pos" select="$pos" /> <xsl:with-param name="current" select="." /> <xsl:with-param name="nodes" select="../product[floor((position() - 1) div 3) = $pos]" /> </xsl:call-template> </xsl:template> <xsl:template name="compare"> <xsl:param name="pos"/> <xsl:param name="current" /> <xsl:param name="nodes" /> <xsl:if test="count($nodes[1]|$current) = count($current)"> <!-- 実際はここら辺にredirect:open要素やredirect:write要素の開始タグ・・ --> <dt><xsl:value-of select="concat($current/../@file_id,string($pos))" /></dt> <xsl:apply-templates select="$nodes/yoso" /> <!-- 実際はここら辺にredirect:open要素やredirect:write要素の終了タグ・・ --> </xsl:if> </xsl:template> <xsl:template match="yoso"> <dd><xsl:value-of select="../@file_id" />:<xsl:value-of select="text()" /></dd> </xsl:template> </xsl:stylesheet> 出力結果 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Q3861990</title> </head> <body> <dl> <dt>a0</dt> <dd>a-01:ああああああああああああああああ</dd> <dd>a-02:いいいいいいいいいいいいいいいい</dd> <dd>a-03:うううううううううううううううう</dd> <dt>a1</dt> <dd>a-04:ああああああああああああああああ</dd> <dd>a-05:いいいいいいいいいいいいいいいい</dd> <dd>a-06:うううううううううううううううう</dd> <dt>b0</dt> <dd>b-01:ああああああああああああああああ</dd> <dd>b-02:いいいいいいいいいいいいいいいい</dd> <dd>b-03:うううううううううううううううう</dd> <dt>b1</dt> <dd>b-04:うううううううううううううううう</dd> <dt>c0</dt> <dd>c-01:ああああああああああああああああ</dd> <dd>c-02:いいいいいいいいいいいいいいいい</dd> <dd>c-03:うううううううううううううううう</dd> </dl> </body> </html> ============================================ 教えてgooの仕様で,URIっぽい文字列の前後にXMLに存在できない空白文字を埋め込まれることが多い。動作しないことがよくあるのでテキストエディタで適宜編集してから使用すること。

関連するQ&A

  • 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を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
  • 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
  • XMLをxalanでXSL-FOに変換する

    book.xmlとbook.xslでbook.foに変換にxalanで実行しました。 >java org.apache.xalan.xslt.Process -in book.xml -xsl book.xsl -out book.fo ところが、以下のようにエラーが出ました。 Exception in thread "main" java.lang.ClassCastException: org.apache.xalan.res.XSLTErrorResources_ja at org.apache.xalan.xslt.Process.main(Process.java:209) 何が原因なのでしょうか?xalan-j_2_4_1のxalan.jarとxercesImpl.jarにはCLASSPATH設定をしています。 <book.xml> <?xml version="1.0" encoding="Shift_JIS" ?> <?xml:stylesheet type="text/xsl" href="book.xsl" ?> <書籍管理> <書籍情報 ID="1"> <書籍名>スタイルシート辞典 第2版</書籍名> <著者>安川英明</著者> <出版社>翔泳社</出版社> <価格>1800</価格> </書籍情報> ・・・ <書籍管理> 文字数が多くなってすべてbook.xslがかけませんでした。 テーブル形式で表示するようしたいのです。 <xsl:template match="書籍管理"><fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> ・・・ <fo:table> <fo:table-column column-width="50mm"/> ・・・ <fo:table-body><fo:table-row><fo:table-cell><fo:block>書籍名</fo:block></fo:table-cell>・・・</fo:table-row> <xsl:apply-templates/> </fo:table-body> </fo:table> </fo:flow></fo:page-sequence> </fo:root></xsl:template> <xsl:template match="書籍情報"> <fo:table-row><xsl:apply-templates/></fo:table-row> </xsl:template> <xsl:template match="書籍名"> <fo:table-cell><fo:block><xsl:value-of select='.'/></fo:block></fo:table-cell> </xsl:template>

    • ベストアンサー
    • 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
  • XMLからXML

    こんにちわ。 以下のやり方がわからなく、困っています。 XSLTを使って以下のようなXML→XMLを行いたいのですが。 変更前: <a> <b> <c>abc</c> <d>def</d> </b> </a> 変更後: <b> <c>abc</c> <d>def</d> </b> ようするに、指定したタグ以下のXML構造をそのまま取り出したいのです。 <xsl:template match="/a"> <xsl:apply-templates select="b"/> </xsl:template> だと中身のabc defしか取れません。 <c>とか<d>は、時によって変わる場合、どのようなXSLT を書けばよいのでしょうか? 申し訳ありませんが、わかる方がいれば教えてください。

    • 締切済み
    • XML
  • xsl:sortを子要素で

    こんにちは。いつもお世話になっています <a hoge="hoge"> <b id="1"> <b id="3"> </a> <a hoge="hogehoge"> <b id="2"> </a> このような構造のxmlで、xslを使用して <b id="1"> <b id="2"> <b id="3"> のような出力を得たいと思っています。 <xsl:apply-templates><xsl:sort select="b" /><xsl:apply-templates> では上手くいきませんでした。 何か方法があるでしょうか

    • ベストアンサー
    • XML
  • XML→PDF出力

    WebページでXMLで表示している内容をPDFとして出力する方法を 教えて下さい。 XML→XSLT→XSL Formatting Object→XSL Formatter→表示 のようにするみたいですが、 Apacheのxerces-j、xalan-j、FOPなどを 用いて実際にはどのようなソース、手順で作成するのか知りたいです。 よろしくお願いします。

    • ベストアンサー
    • 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>

  • 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

専門家に質問してみよう