XSLTを使ってみる(J2SE 5.0)

戻る

::::::::::::::
XSLT.java
::::::::::::::
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

/**
        $Id: xslt.html,v 1.1 2009/06/22 16:12:33 kishi Exp kishi $
	@author KISHI Yasuhiro
*/

public class XSLT {

    public static void main( String[] args ) {

        String xslFile = null;
        String xmlFile = null;

        if ( args.length != 2 ) {
            System.out.println( "java -cp . XSLT [xsl] [xml]" );
            System.exit( 1 );
        }

        xslFile = args[ 0 ];
        xmlFile = args[ 1 ];

        try {
            // TransformerFactoryインスタンスを取得
            TransformerFactory factory = TransformerFactory.newInstance();

            // XSLファイルからtranceformerを取得
            Transformer transformer =
                factory.newTransformer( new StreamSource( xslFile ) );

            // 出力するエンコーディングを設定
            transformer.setOutputProperty( "encoding", "Shift_JIS" );

            // XMLファイルをXSLTで変換して出力
            transformer.transform( new StreamSource( xmlFile ),
                                   new StreamResult( System.out ) );

            System.out.println();

        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}

::::::::::::::
test.xsl
::::::::::::::
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.0" encoding="Shift_JIS" indent="yes"/>

<xsl:template match="/">

<html>

<head>
	<xsl:comment>
	headタグを書き込むと勝手にMETAタグが挿入されるようです。
	</xsl:comment>
	<title>私のすきな音楽</title>
</head>

<body>

<center>
	<xsl:comment>
	cdList要素のdescription属性の値を表示します。
	</xsl:comment>
        <xsl:value-of select="cdList/@description"/>
</center>

<table border="1" cellpadding="1" cellspacing="1">
        <xsl:for-each select="cdList/sourceName" >
                <tr>
                <td>
			<xsl:comment>
			cdList/title要素の値を表示します。
			</xsl:comment>
                <xsl:value-of select="title"/>
                </td>
                <td>
			<xsl:comment>
			cdList/artist要素の値を表示します。
			</xsl:comment>
                <xsl:value-of select="artist"/>
                </td>
                </tr>
        </xsl:for-each>
</table>

</body>
</html>

</xsl:template>
</xsl:stylesheet>

::::::::::::::
test.xml
::::::::::::::
<?xml version="1.0" encoding="Shift_JIS" ?>

<cdList description="私の愛聴CD">
        <sourceName>
                <title>リターン・トゥ・フォエバー</title>
                <artist>Chick Armando Corea</artist>
        </sourceName>
        <sourceName>
                <title>マン・チャイルド</title>
                <artist>Herbert Hancock</artist>
        </sourceName>
</cdList>

::::::::::::::
test.html
::::::::::::::
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<!--
	headタグを書き込む勝手にMETAタグが挿入されるようです。
	-->
<title>私のすきな音楽</title>
</head>
<body>
<center>
<!--
	cdList要素のdescription属性の値を表示します。
	-->私の愛聴CD</center>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
<!--
			cdList/title要素の値を表示します。
			-->リターン・トゥ・フォエバー</td><td>
<!--
			cdList/artist要素の値を表示します。
			-->Chick Armando Corea</td>
</tr>
<tr>
<td>
<!--
			cdList/title要素の値を表示します。
			-->マン・チャイルド</td><td>
<!--
			cdList/artist要素の値を表示します。
			-->Herbert Hancock</td>
</tr>
</table>
</body>
</html>

戻る inserted by FC2 system