「xslt」の編集履歴(バックアップ)一覧はこちら

xslt」(2007/05/01 (火) 15:54:57) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

-[[TopPage]] ***xsltまとめ。 ---- ***javascriptでのxsltの利用方法。(firefox用) 参考ページ:[[JavaScriptでXSLT - XMLHttpRequestのレスポンスをXSLTで処理>http://web.paulownia.jp/script/xml/xslt.html]] ※xslにxmlns="http://www.w3.org/1999/xhtml" を追加しないと、正しく表示されず。  xsl内でjavascriptが使えるかが不明。 javascriptコード <html> <head> <title>Untitled</title> <Script LANGUAGE="JavaScript"> <!-- function clickFirefoxTest(){ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = readystatechange; xmlhttp.open("GET", "testxml.xml", true); xmlhttp.send(null); function readystatechange(){ if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var xml = xmlhttp.responseXML; var xslt = new XSLTProcessor(); var xsltDoc = document.implementation.createDocument("", "", null); xsltDoc.async = false; xsltDoc.load("testxsl.xsl") xslt.importStylesheet(xsltDoc); var fragment = document.implementation.createDocument("", "", null); var doc = xslt.transformToFragment(xml, fragment); var target = document.getElementById("target") while (target.hasChildNodes()) { target.removeChild(target.lastChild); } target.appendChild(doc); } } } // --> </Script> </head> <body> <form action="."> <p><input type="button" onclick="clickFirefoxTest();" value="Firefox" /></p> </form> <div id="target"></div> </body> </html> 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" xmlns:yahoo="urn:yahoo:jp:auc:leaf" xml:lang="ja"> <xsl:template match="/yahoo:AuctionCategoryLeaf"> <table> <caption>aaa</caption> <xsl:apply-templates /> </table> </xsl:template> <xsl:template match="yahoo:item"> <tr> <td><xsl:value-of select="yahoo:title" /></td> </tr> </xsl:template> </xsl:stylesheet> ---- ***Parameterの渡し方について。(servlet→xsl) xsl内に<xsl:param name="timeParam" select="1"/>を宣言。 servelt内で、 int paramTime = 1; Transformer transformer = factory.newTransformer(xslSource); //Parameterを付与 Object obj = new Integer(paramTime); transformer.setParameter("timeParam", obj); transformer.transform(xmlSource, new StreamResult(bw)); ※setParameterに渡すのは、param名とObjectである点に注意。 ---- ***2007/02/22xsl変更(ファルダxalan内) -xalanのエクステンションについて <xsl:variable name="dateobj" select="util:get($calender,5)"/>のところで、Calender.DATEを指定しようとしたが、うまく行かず、 直接5を指定している。javaのメソッドを直接呼び出せるが、フィールドは難しい?やはり、拡張classを作るべきか。 -追記:2007/02/22 通常のjavaのクラスライブラリへ、オリジナルのパッケージを登録することで、ある程度の問題を解決。    [[java拡張機能参考url>http://www.atmarkit.co.jp/fjava/onepoint/java/jv_ext.html]]    [[jar形式参考url>http://www.nextindex.net/java/jar.html]] C:\j2sdk1.4.2_08\jre\lib\ext と C:\Program Files\Java\jre1.5.0_09\lib\ext にjar形式のパッケージを追加。 追記:C:\Program Files\Java\jre1.5.0_11\lib\extの方に追加しないと実行しない場合がある? xlsの変更点。 xmlns:date="xalan://mytest"を追加。 <xsl:template match="yahoo:item"> <xsl:variable name="dateobj" select="date:test.StringDate.new()"/> ←変更 <xsl:variable name="datesb" select="date:getDate($dateobj)"/>   ←変更 <xsl:if test="contains(yahoo:endtime,concat($datesb,'時'))"> -nowrapについて。 htmlにおいて、<td nowrap>はxmlでは、厳格に<td nowrap="nowrap">と記述する必要がる。 -参考url   [[xalanのextentionについて>http://www.teria.com/~koseki/emacswiki/XalanExtensionFunctions.html]] <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:yahoo="urn:yahoo:jp:auc:search" xmlns:util="xalan://java.util" xmlns:lang="xalan://java.lang"> <xsl:template match="/yahoo:AuctionSearchResult"> <html> <head> <title>xsltest</title> </head> <body> <table CELLPADDING="2" CELLSPACING="0" BORDER="2" WIDTH="100%"> <tr BGCOLOR="#dcdcdc"> <td ALIGN="center" nowrap="nowrap"><small>タイトル</small></td> <td ALIGN="center" nowrap="nowrap"><small>現在価格</small></td> <td ALIGN="center" nowrap="nowrap"><small>入札</small></td> <td ALIGN="center" nowrap="nowrap"><small>終了予定時間</small></td> </tr> <xsl:apply-templates /> </table> </body> </html> </xsl:template> <xsl:template match="yahoo:item"> <xsl:variable name="calender" select="util:Calendar.getInstance()"/> <xsl:variable name="dateobj" select="util:get($calender,5)"/> <xsl:variable name="datesb" select="lang:Integer.toString($dateobj)"/> <xsl:if test="contains(yahoo:endtime,concat($datesb,'日'))"> <tr BGCOLOR="#eeeeee"> <td ALIGN="left" nowrap="nowrap"><a><xsl:attribute name="href"><xsl:value-of select="yahoo:url" /></xsl:attribute><xsl:value-of select="yahoo:title" /></a></td> <td ALIGN="right" nowrap="nowrap"><b><xsl:value-of select="yahoo:price" /></b></td> <td ALIGN="center" nowrap="nowrap"><b><xsl:value-of select="yahoo:bids" /></b></td> <td ALIGN="right" nowrap="nowrap"><xsl:value-of select="yahoo:endtime" /></td> </tr> <tr> <td BGCOLOR="#ffffff" COLSPAN="5"> <img align="left" border="0" hspace="5" width="64" height="65"><xsl:attribute name="src"><xsl:value-of select="yahoo:img" /></xsl:attribute></img> </td> </tr> <tr BGCOLOR="#eeeeee"><td COLSPAN="5"><img SRC="http://a1017.lm.a.yimg.com/7/1017/1783/000/image.auctions.yahoo.co.jp/images/space.gif" WIDTH="1" HEIGHT="1"></img></td></tr> </xsl:if> </xsl:template> </xsl:stylesheet> ---- ***servletでの参考プログラム。 import java.net.*; import java.io.*; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; public class xslttestservlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ //PrintWriter outより手前で記述しておかないと、文字化けしてしまう。 response.setContentType("text/html; charset=Shift-Jis"); //tyr~catch内に記述しておくとエラーになる。 ← 要確認。 PrintWriter out = response.getWriter(); try{ //request.setCharacterEncoding(""); //URLクラスで、ストリームをオープン。検索結果を取得(XML) URL url_yahoo = new URL("http://api.auctions.yahoo.co.jp/AuctionWebService/V1/Search?appid=syanporion&query=win2000"); InputStream in_xml = new BufferedInputStream(url_yahoo.openStream()); TransformerFactory factory = TransformerFactory.newInstance(); //javax.servlet.ServletContext:サーブレットの設定等を取得するためのインターフェイス。getServletContext().getRealPath()でサーバーのシステム内でのコンテキストのpathを返す。 //()内には、コンテキスト以下のファイルのpathを入力。 ServletContext context = this.getServletContext(); String realPath = this.getServletContext().getRealPath("xsltest.xsl"); Source xmlSource = new StreamSource(in_xml); Source xslSource = new StreamSource(new URL("file:"+realPath).openStream()); Transformer transformer = factory.newTransformer(xslSource); transformer.transform(xmlSource, new StreamResult(out)); } catch (Exception e) { out.write(e.getMessage()); e.printStackTrace(out); } out.close(); } } -サーバーへの配備上の注意。 chmod 755 で、warファイルに実行権限を与えておく。 tomcat、httpdを再起動。(server.xml辺りで、再起動しないでもよくできたはず。) サーブレットへIEからアクセス。 このとき":8080"をドメインの後ろに追加して、tomcatのみで動作させるとエラーログを吐き出す。実運用では8080を閉じておくべき。 -web.xmlの例。 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>xslttest</servlet-name> <servlet-class>xslttestservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xslttest</servlet-name> <url-pattern>/xslttest</url-pattern> </servlet-mapping> </web-app> ---- ***コマンドラインでの参考プログラム。 -args[1]がxsl、args[2]がxmlを指定して、xmlをxsl指定のhtmlに変換するコマンドアプリケーション例。[[RADKRAFT参照>http://kamakura.cool.ne.jp/oppama/yxml/xalan-programming.html]] import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class xslttest { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { String xmlFileName, xslFileName; if(args.length != 2){ System.err.println("usage: Xalan2 in.xsl in.xml > out.html"); return; } xslFileName = args[0]; xmlFileName = args[1]; TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xslFileName)); //transformer.transform(new StreamSource(xmlFileName), new StreamResult(System.out)); transformer.transform(new StreamSource(xmlFileName), new StreamResult(new java.io.FileOutputStream("xmltest.html"))); } } -xslファイル <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:yahoo="urn:yahoo:jp:auc:search"> <xsl:template match="/yahoo:AuctionSearchResult"> <html> <head> <title>xsltest</title> </head> <body> <p align="center">xlsttest</p> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="yahoo:item"> <p><xsl:value-of select="yahoo:title" /></p> </xsl:template> </xsl:stylesheet> ※ 説明のxsl記述では、全部のテキスト情報が表示され上手く行かなかった。   そこで、xmlns:yahoo="urn:yahoo:jp:auc:search"を追加して、各ノード名の後ろにnodename:yahooと追加したとことうまく動作した。   xsd、targetnamespaceなどが関連していそう。要調査。     [[@IT参照>http://www.atmarkit.co.jp/fdotnet/dotnettips/477aspxmldatasrc/aspxmldatasrc.html]] -xmlファイルは、yahooオークションAPIでの検索結果を利用。
***xsltまとめ。 ---- ***javascriptでのxsltの利用方法。(firefox用) 参考ページ:[[JavaScriptでXSLT - XMLHttpRequestのレスポンスをXSLTで処理>http://web.paulownia.jp/script/xml/xslt.html]] ※xslにxmlns="http://www.w3.org/1999/xhtml" を追加しないと、正しく表示されず。  xsl内でjavascriptが使えるかが不明。 javascriptコード <html> <head> <title>Untitled</title> <Script LANGUAGE="JavaScript"> <!-- function clickFirefoxTest(){ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = readystatechange; xmlhttp.open("GET", "testxml.xml", true); xmlhttp.send(null); function readystatechange(){ if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var xml = xmlhttp.responseXML; var xslt = new XSLTProcessor(); var xsltDoc = document.implementation.createDocument("", "", null); xsltDoc.async = false; xsltDoc.load("testxsl.xsl") xslt.importStylesheet(xsltDoc); var fragment = document.implementation.createDocument("", "", null); var doc = xslt.transformToFragment(xml, fragment); var target = document.getElementById("target") while (target.hasChildNodes()) { target.removeChild(target.lastChild); } target.appendChild(doc); } } } // --> </Script> </head> <body> <form action="."> <p><input type="button" onclick="clickFirefoxTest();" value="Firefox" /></p> </form> <div id="target"></div> </body> </html> 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" xmlns:yahoo="urn:yahoo:jp:auc:leaf" xml:lang="ja"> <xsl:template match="/yahoo:AuctionCategoryLeaf"> <table> <caption>aaa</caption> <xsl:apply-templates /> </table> </xsl:template> <xsl:template match="yahoo:item"> <tr> <td><xsl:value-of select="yahoo:title" /></td> </tr> </xsl:template> </xsl:stylesheet> ---- ***Parameterの渡し方について。(servlet→xsl) xsl内に<xsl:param name="timeParam" select="1"/>を宣言。 servelt内で、 int paramTime = 1; Transformer transformer = factory.newTransformer(xslSource); //Parameterを付与 Object obj = new Integer(paramTime); transformer.setParameter("timeParam", obj); transformer.transform(xmlSource, new StreamResult(bw)); ※setParameterに渡すのは、param名とObjectである点に注意。 ---- ***2007/02/22xsl変更(ファルダxalan内) -xalanのエクステンションについて <xsl:variable name="dateobj" select="util:get($calender,5)"/>のところで、Calender.DATEを指定しようとしたが、うまく行かず、 直接5を指定している。javaのメソッドを直接呼び出せるが、フィールドは難しい?やはり、拡張classを作るべきか。 -追記:2007/02/22 通常のjavaのクラスライブラリへ、オリジナルのパッケージを登録することで、ある程度の問題を解決。    [[java拡張機能参考url>http://www.atmarkit.co.jp/fjava/onepoint/java/jv_ext.html]]    [[jar形式参考url>http://www.nextindex.net/java/jar.html]] C:\j2sdk1.4.2_08\jre\lib\ext と C:\Program Files\Java\jre1.5.0_09\lib\ext にjar形式のパッケージを追加。 追記:C:\Program Files\Java\jre1.5.0_11\lib\extの方に追加しないと実行しない場合がある? xlsの変更点。 xmlns:date="xalan://mytest"を追加。 <xsl:template match="yahoo:item"> <xsl:variable name="dateobj" select="date:test.StringDate.new()"/> ←変更 <xsl:variable name="datesb" select="date:getDate($dateobj)"/>   ←変更 <xsl:if test="contains(yahoo:endtime,concat($datesb,'時'))"> -nowrapについて。 htmlにおいて、<td nowrap>はxmlでは、厳格に<td nowrap="nowrap">と記述する必要がる。 -参考url   [[xalanのextentionについて>http://www.teria.com/~koseki/emacswiki/XalanExtensionFunctions.html]] <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:yahoo="urn:yahoo:jp:auc:search" xmlns:util="xalan://java.util" xmlns:lang="xalan://java.lang"> <xsl:template match="/yahoo:AuctionSearchResult"> <html> <head> <title>xsltest</title> </head> <body> <table CELLPADDING="2" CELLSPACING="0" BORDER="2" WIDTH="100%"> <tr BGCOLOR="#dcdcdc"> <td ALIGN="center" nowrap="nowrap"><small>タイトル</small></td> <td ALIGN="center" nowrap="nowrap"><small>現在価格</small></td> <td ALIGN="center" nowrap="nowrap"><small>入札</small></td> <td ALIGN="center" nowrap="nowrap"><small>終了予定時間</small></td> </tr> <xsl:apply-templates /> </table> </body> </html> </xsl:template> <xsl:template match="yahoo:item"> <xsl:variable name="calender" select="util:Calendar.getInstance()"/> <xsl:variable name="dateobj" select="util:get($calender,5)"/> <xsl:variable name="datesb" select="lang:Integer.toString($dateobj)"/> <xsl:if test="contains(yahoo:endtime,concat($datesb,'日'))"> <tr BGCOLOR="#eeeeee"> <td ALIGN="left" nowrap="nowrap"><a><xsl:attribute name="href"><xsl:value-of select="yahoo:url" /></xsl:attribute><xsl:value-of select="yahoo:title" /></a></td> <td ALIGN="right" nowrap="nowrap"><b><xsl:value-of select="yahoo:price" /></b></td> <td ALIGN="center" nowrap="nowrap"><b><xsl:value-of select="yahoo:bids" /></b></td> <td ALIGN="right" nowrap="nowrap"><xsl:value-of select="yahoo:endtime" /></td> </tr> <tr> <td BGCOLOR="#ffffff" COLSPAN="5"> <img align="left" border="0" hspace="5" width="64" height="65"><xsl:attribute name="src"><xsl:value-of select="yahoo:img" /></xsl:attribute></img> </td> </tr> <tr BGCOLOR="#eeeeee"><td COLSPAN="5"><img SRC="http://a1017.lm.a.yimg.com/7/1017/1783/000/image.auctions.yahoo.co.jp/images/space.gif" WIDTH="1" HEIGHT="1"></img></td></tr> </xsl:if> </xsl:template> </xsl:stylesheet> ---- ***servletでの参考プログラム。 import java.net.*; import java.io.*; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; public class xslttestservlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ //PrintWriter outより手前で記述しておかないと、文字化けしてしまう。 response.setContentType("text/html; charset=Shift-Jis"); //tyr~catch内に記述しておくとエラーになる。 ← 要確認。 PrintWriter out = response.getWriter(); try{ //request.setCharacterEncoding(""); //URLクラスで、ストリームをオープン。検索結果を取得(XML) URL url_yahoo = new URL("http://api.auctions.yahoo.co.jp/AuctionWebService/V1/Search?appid=syanporion&query=win2000"); InputStream in_xml = new BufferedInputStream(url_yahoo.openStream()); TransformerFactory factory = TransformerFactory.newInstance(); //javax.servlet.ServletContext:サーブレットの設定等を取得するためのインターフェイス。getServletContext().getRealPath()でサーバーのシステム内でのコンテキストのpathを返す。 //()内には、コンテキスト以下のファイルのpathを入力。 ServletContext context = this.getServletContext(); String realPath = this.getServletContext().getRealPath("xsltest.xsl"); Source xmlSource = new StreamSource(in_xml); Source xslSource = new StreamSource(new URL("file:"+realPath).openStream()); Transformer transformer = factory.newTransformer(xslSource); transformer.transform(xmlSource, new StreamResult(out)); } catch (Exception e) { out.write(e.getMessage()); e.printStackTrace(out); } out.close(); } } -サーバーへの配備上の注意。 chmod 755 で、warファイルに実行権限を与えておく。 tomcat、httpdを再起動。(server.xml辺りで、再起動しないでもよくできたはず。) サーブレットへIEからアクセス。 このとき":8080"をドメインの後ろに追加して、tomcatのみで動作させるとエラーログを吐き出す。実運用では8080を閉じておくべき。 -web.xmlの例。 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>xslttest</servlet-name> <servlet-class>xslttestservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xslttest</servlet-name> <url-pattern>/xslttest</url-pattern> </servlet-mapping> </web-app> ---- ***コマンドラインでの参考プログラム。 -args[1]がxsl、args[2]がxmlを指定して、xmlをxsl指定のhtmlに変換するコマンドアプリケーション例。[[RADKRAFT参照>http://kamakura.cool.ne.jp/oppama/yxml/xalan-programming.html]] import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class xslttest { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { String xmlFileName, xslFileName; if(args.length != 2){ System.err.println("usage: Xalan2 in.xsl in.xml > out.html"); return; } xslFileName = args[0]; xmlFileName = args[1]; TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xslFileName)); //transformer.transform(new StreamSource(xmlFileName), new StreamResult(System.out)); transformer.transform(new StreamSource(xmlFileName), new StreamResult(new java.io.FileOutputStream("xmltest.html"))); } } -xslファイル <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:yahoo="urn:yahoo:jp:auc:search"> <xsl:template match="/yahoo:AuctionSearchResult"> <html> <head> <title>xsltest</title> </head> <body> <p align="center">xlsttest</p> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="yahoo:item"> <p><xsl:value-of select="yahoo:title" /></p> </xsl:template> </xsl:stylesheet> ※ 説明のxsl記述では、全部のテキスト情報が表示され上手く行かなかった。   そこで、xmlns:yahoo="urn:yahoo:jp:auc:search"を追加して、各ノード名の後ろにnodename:yahooと追加したとことうまく動作した。   xsd、targetnamespaceなどが関連していそう。要調査。     [[@IT参照>http://www.atmarkit.co.jp/fdotnet/dotnettips/477aspxmldatasrc/aspxmldatasrc.html]] -xmlファイルは、yahooオークションAPIでの検索結果を利用。

表示オプション

横に並べて表示:
変化行の前後のみ表示: