目次
データベース
サンプル6 データセットをXSLTを使って簡易的なHTML出力
データセットのテーブルをXSLTを使って簡単なHTML出力をします。
こんなデータセットが。
dataSetLink1["Category"] テーブル(スキーマは使いません)レコード
| ID | UserID | Title | Visible | Description |
|---|---|---|---|---|
| 1 | 1 | Links | 1 | (null) |
dataSetLink1["Link"] テーブル(スキーマは使いません)
レコード
| ID | UserID | CategoryID | Title | WebUrl | RssUrl | Visible | NewWindow |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | #えムナウのプログラミングのページ10 | http://www.geocities.jp/mnow | (null) | 1 | 0 |
| 2 | 1 | 1 | #えムナウのプログラミングのページ11 | http://www.geocities.jp/mnow | (null) | 1 | 1 |
| 3 | 1 | 1 | #えムナウのプログラミングのページ10 | http://www.geocities.jp/mnow | http://www.geocities.jp/mnow/rss | 1 | 0 |
| 4 | 1 | 1 | #えムナウのプログラミングのページ11 | http://www.geocities.jp/mnow | http://www.geocities.jp/mnow/rss | 1 | 1 |
こんなHTMLファイルになります。
<DIV>
<H1 class="listtitle">Links</H1>
<UL class="list">
<LI class="listitem"><A class="listitem" href="http://www.geocities.jp/mnow" target="_top">#えムナウのプログラミングのページ10</A></LI>
<LI class="listitem"><A class="listitem" href="http://www.geocities.jp/mnow" target="_blank">#えムナウのプログラミングのページ11</A></LI>
<LI class="listitem"><A class="listitem" href="http://www.geocities.jp/mnow" target="_top">#えムナウのプログラミングのページ10</A> <A class="listitem" href="http://www.geocities.jp/mnow/rss" target="_blank">(RSS)</A></LI>
<LI class="listitem"><A class="listitem" href="http://www.geocities.jp/mnow" target="_blank">#えムナウのプログラミングのページ11</A> <A class="listitem" href="http://www.geocities.jp/mnow/rss" target="_blank">(RSS)</A></LI>
</UL>
</DIV>
ソースです。
ボタンを押した時にHTMLファイルを作成します。
XSLTを変更、ボタン押下を繰り返せるようにしています。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
namespace HtmlOutput
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private XmlDataDocument xmlDoc ;
private DataSet dataSetLink1 ;
private System.Windows.Forms.Button button1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2;
private System.Data.SqlClient.SqlCommand sqlSelectCommand2;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
dataSetLink1 = new DataSet("DataSetLink");
dataSetLink1.Clear() ;
sqlDataAdapter1.SelectCommand.Parameters["@Param1"].Value = "1" ;
sqlDataAdapter1.SelectCommand.Parameters["@Param2"].Value = "1" ;
sqlDataAdapter1.Fill(dataSetLink1,"Category") ;
sqlDataAdapter2.SelectCommand.Parameters["@Param3"].Value = "1" ;
sqlDataAdapter2.SelectCommand.Parameters["@Param4"].Value = "1" ;
sqlDataAdapter2.Fill(dataSetLink1,"Link") ;
xmlDoc = new XmlDataDocument(dataSetLink1);
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
((System.ComponentModel.ISupportInitialize)(this.dataSetLink1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// sqlDataAdapter2
//
this.sqlDataAdapter2.SelectCommand = this.sqlSelectCommand2;
this.sqlDataAdapter2.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Link", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("Title", "Title"),
new System.Data.Common.DataColumnMapping("WebUrl", "WebUrl"),
new System.Data.Common.DataColumnMapping("RssUrl", "RssUrl"),
new System.Data.Common.DataColumnMapping("Visible", "Visible"),
new System.Data.Common.DataColumnMapping("NewWindow", "NewWindow")})});
//
// sqlSelectCommand2
//
this.sqlSelectCommand2.CommandText = "SELECT ID, Title, WebUrl, RssUrl, Visible, NewWindow FROM Link WHERE (UserID = @P" +
"aram3) AND (CategoryID = @Param4)";
this.sqlSelectCommand2.Connection = this.sqlConnection1;
this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.Int, 4, "UserID"));
this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.Int, 4, "CategoryID"));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=\"DELL-XEON\";packet size=4096;integrated security=SSPI;data source=" +
"\"DELL-XEON\";persist security info=False;initial catalog=CMSTEST";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Category", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("Title", "Title"),
new System.Data.Common.DataColumnMapping("Visible", "Visible"),
new System.Data.Common.DataColumnMapping("Description", "Description")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT Title, Visible, Description FROM Category WHERE (UserID = @Param1) AND (ID" +
" = @Param2)";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param1", System.Data.SqlDbType.Int, 4, "UserID"));
this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param2", System.Data.SqlDbType.Int, 4, "ID"));
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataSetLink1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
XslTransform xslTran = new XslTransform();
xslTran.Load(Application.StartupPath + @"\XSLTFile1.xsl");
XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + @"\output.htm", System.Text.Encoding.UTF8);
xslTran.Transform(xmlDoc, null, writer, null);
writer.Close();
}
catch ( Exception ex )
{
string s = ex.Message ;
}
}
}
}
XSLTFile1.xslt XSLTファイルです。
実行ファイルと同じ場所においてください。
改行を入れるための</xsl:text>のせいでインデントが崩れてしまいます。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<DIV><xsl:text>
</xsl:text>
<xsl:apply-templates select="/DataSetLink/Category"/>
</DIV><xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="/DataSetLink/Category">
<xsl:if test="Visible='true'">
<H1 class="listtitle"><xsl:value-of select="Title"/></H1><xsl:text>
</xsl:text>
<UL class="list"><xsl:text>
</xsl:text>
<xsl:apply-templates select="/DataSetLink/Link"/>
</UL><xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="/DataSetLink/Link">
<xsl:if test="Visible='true'">
<LI class="listitem">
<xsl:choose>
<xsl:when test="NewWindow='true'">
<A class="listitem" href="{WebUrl}" target="_blank">
<xsl:value-of select="Title"/>
</A>
</xsl:when>
<xsl:when test="NewWindow='false'">
<A class="listitem" href="{WebUrl}" target="_top">
<xsl:value-of select="Title"/>
</A>
</xsl:when>
</xsl:choose>
<xsl:if test="RssUrl!=''">
<xsl:text> </xsl:text>
<A class="listitem" href="{RssUrl}" target="_blank">(RSS)</A>
</xsl:if>
</LI><xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>



