フォームの生成

<xsl:element><xsl:attribute>を利用し、フォーム要素のHTMLタグとその属性を創りだす。属性値は直接記述してもよいが、XML文書から取り出すのがよい。

[サンプルXML文書を表示](下のフレームに)

(formのaction属性を設定していないので、実際に送信はされません)

XMLソース

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="form01.xsl"?>
<root>
<title>KodayanHPのサイト一覧</title>
<contents>
<list>
<main>大阪日本橋PCショップ</main>
<sub><value>1</value>日本橋Map</sub>
<sub><value>2</value>PCショップ名一覧</sub>
<sub><value>3</value>PCショップ価格比較</sub>
<sub><value>4</value>日本橋格付けランク</sub>
<sub><value>5</value>日本橋関連リンク集</sub>
</list>
<list>
<main>Webプログラミング</main>
<sub><value>1</value>CSSとDynamicHTML</sub>
<sub><value>2</value>PerでCGI</sub>
<sub><value>3</value>XML</sub>
<sub><value>4</value>Java Script</sub>
<sub><value>5</value>Java Applet</sub>
<sub><value>6</value>VB SCript</sub>
</list>
<list>
<main>その他のコンテンツ</main>
<sub><value>1</value>お気に入りリンク集</sub>
<sub><value>2</value>お知らせ</sub>
<sub><value>3</value>ココログ(blog)</sub>
<sub><value>4</value>HPの履歴</sub>
<sub><value>5</value>アクセスログ解析報告</sub>
<sub><value>6</value>アンケート</sub>
</list>
</contents>
</root>

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="html" encoding="Shift_JIS"/>

<xsl:template match="/">
<center><H3>フォームの要素を創ってみよう!</H3></center>
<form>
<xsl:apply-templates select="root/contents"/>
</form>
</xsl:template>

<xsl:template match="contents">
<h5>(1)1行テキストボックスをつくる</h5>
名前欄:<xsl:element name="input">
<xsl:attribute name="type">text</xsl:attribute>
<xsl:attribute name="name">txtbox</xsl:attribute>
<xsl:attribute name="value">(お名前は?)</xsl:attribute>
<xsl:attribute name="size">15</xsl:attribute>
</xsl:element>
<h5>(2)複数行テキストボックスをつくる</h5>
コメント欄:<xsl:element name="textarea">
<xsl:attribute name="name">txtarea</xsl:attribute>
<xsl:attribute name="rows">3</xsl:attribute>
<xsl:attribute name="cols">50</xsl:attribute>
(ご意見をどうぞ!)
</xsl:element>
<h5>(3)ラジオボタンをつくる</h5>
<xsl:for-each select="list[position()=1]/sub">
<xsl:element name="input">
<xsl:attribute name="type">radio</xsl:attribute>
<xsl:attribute name="name">PC</xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="value"/></xsl:attribute>
<xsl:if test="position() = 1">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
<xsl:value-of select="text()"/>
</xsl:element><br/>
</xsl:for-each>
<h5>(4)チェックボックスをつくる</h5>
<xsl:for-each select="list[position()=2]/sub">
<xsl:element name="input">
<xsl:attribute name="type">checkbox</xsl:attribute>
<xsl:attribute name="name">WEB</xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="value"/></xsl:attribute>
<xsl:if test="position() = 3">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
<xsl:value-of select="text()"/>
</xsl:element><br/>
</xsl:for-each>
<h5>(5)ドロップダウン選択リストをつくる</h5>
<xsl:element name="select">
<xsl:attribute name="name">ETC</xsl:attribute>
<xsl:for-each select="list[position()=3]/sub">
<xsl:element name="option">
<xsl:attribute name="value"><xsl:value-of select="value"/></xsl:attribute>
<xsl:if test="position() = 2">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:for-each>
</xsl:element>
<hr/>
<xsl:element name="input">
<xsl:attribute name="type">submit</xsl:attribute>
<xsl:attribute name="value">送信</xsl:attribute>
<xsl:attribute name="onclick">alert("フォーム要素を創りだすテストです、
そのため送信機能は付けてません!")</xsl:attribute>
</xsl:element>
<xsl:element name="input">
<xsl:attribute name="type">reset</xsl:attribute>
<xsl:attribute name="value">クリア</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

end(04/6/4)