まず、下記の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)とかにできれば、
なにかみえてきそうなきもするんですが、、、、
長々と申し訳ありませんが、
ご協力お願いします。
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が見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- PHP imageフォルダに、画像をリサイズして保存する時のファイル名を変更したい 1 2023/05/30 11:39
- JavaScript jqueryを使ったスムーススクロールのコードを書いたのですが、HTMLコード内にある、a butt 2 2022/04/14 10:59
- JavaScript switch文のswitch(n)の部分を複数の値にするか、if文に変えてほしいです。 1 2022/07/27 17:18
- JavaScript セレクトボックスを2つ設けて選択して初めてメッセを表示 1 2022/07/27 12:15
- その他(プログラミング・Web制作) セレクトボックスで選択された値をコントローラーで使用したい 2 2022/07/26 16:41
- その他(プログラミング・Web制作) pythonのエラーについて 2 2022/08/17 17:17
- CGI htmlからパラメータで、cgiに渡したい。 1 2023/02/06 16:15
- MySQL php テーブルを作れない 2 2022/11/17 18:22
- PHP php テーブルが作成できない 1 2022/11/17 23:41
- 格安スマホ・SIMフリースマホ 楽天モバイル OPPO A55s 5G この機種は NNP したら格安スマホどこでもできる 4 2022/06/29 10:17
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
XSLで、XMLの空タグを制御したい。
-
ノードの並び替え
-
タグの有無の判定
-
xslでの最小値、最大値の取得
-
xmlのテキストにリンクを張りた...
-
XMLのHTMLへの変換 (初心者)
-
CPUの考え方を教えてください ...
-
XMLで要素が記述された順番に意...
-
バッチファイルでテキストファ...
-
昔Winnyってありましたけど、あ...
-
あせんうぶり言語
-
VB6でXMLを作成しているのです...
-
SNMP リンクダウンとノードダ...
-
あるノードリストに、特定の名...
-
HTTPステータス 404 - There is...
-
コンテキストメニュークリック...
-
Listからのnull要素を削除方法は?
-
ルート要素ノードが2個ある場合?
-
ノードとは
-
このエラー、何とかなりません...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
XSLで、XMLの空タグを制御したい。
-
タグの有無の判定
-
XSLでXMLデータをタブ区切りデ...
-
XSLTで固定長データファイルを...
-
XSL中の改行、タブを無効にした...
-
xmlのテキストにリンクを張りた...
-
重複するものを消したい
-
XSLTがブラウザで表示されません。
-
うまく表示されない
-
文字列の取得
-
xsl:param要素をxsl:templateの...
-
value-ofの値を属性値とするには
-
XMLをXSLを使いHTMLに変換した...
-
XMLから指定テキストのみを抜き...
-
ある文字列を含むノードが存在...
-
VBAからxml・スタイルシートを...
-
xsl、xpath式の書き方について
-
XSLで特定のタグだけを出力する...
-
XSL内でJavaScriptを記述し、Ja...
-
XSLTで直下のテキストのみ取得...
おすすめ情報