パラメータをXSLTスタイルシートに渡す
パラメータをXSLTスタイルシートに渡す方法は、メソッドや関数に渡す方法と似ています。パラメータをスタイルシートに渡すと、<xsl:stylesheet>要素内の任意の<xsl:param>として定義されているグローバルスコープ変数を初期化することができます。以下のコードでは、パラメータを宣言して使用している部分を太字で示します。
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:param name="BackGroundColor" select="Blue" />
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Passing Parameters to an XSLT Style Sheet</TITLE>
</HEAD>
<BODY>
<H2> Passing Parameters to an XSLT Style Sheet</H2>
<table border="1" cellSpacing="1" cellPadding="1">
<center>
<xsl:for-each select="//Categories">
<!-- Each record on a seperate row -->
<xsl:element name="tr">
<xsl:attribute name="bgcolor">
<xsl:value-of select="$BackGroundColor" />
</xsl:attribute>
<xsl:element name="td">
<xsl:value-of select="ProductSubcategoryID" />
</xsl:element>
<xsl:element name="td">
<xsl:value-of select="Name" />
</xsl:element>
<xsl:element name="td">
<xsl:attribute name="align">center</xsl:attribute>
<xsl:value-of select="ModifiedDate" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</center>
</table>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
このコードでは、BackGroundColorパラメータの宣言の後で、BackGroundColorパラメータを使ってテーブルの<tr>要素の背景色を指定しています。
<xsl:value-of select="$BackGroundColor" />
この例では、< xsl:param>要素でBackGroundColorを宣言するときに値をハードコーディングしましたが、理想的には、この値を呼び出し側ASP.NETページから渡す必要があります。それには、XsltArgumentListクラスのAddParam()メソッドを使用します。XsltArgumentListクラスは、パラメータをXSLTスタイルシートに渡すメカニズムを提供し、再利用可能かつ管理可能なXSLTスタイルシートを作成できるようにするキークラスです。
AddParam()メソッドには、修飾名と名前空間URIと値を引き渡します。String、Boolean、Number、Node Fragment、またはNodeSet以外のパラメータ値は、倍精度浮動小数点型か文字列型になります。パラメータをスタイルシートに渡す、呼び出し側ASP.NETページを次に示します。
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Xml.Xsl" %> <%@ Import Namespace="System.Xml.XPath" %> <%@ Import Namespace="System.Web.Configuration" %> <script runat="server"> void Page_Load(object sender, System.EventArgs e) { string connString = WebConfigurationManager.ConnectionStrings ["adventureWorks"].ConnectionString; using (SqlConnection connection = new SqlConnection(connString)) { connection.Open(); SqlCommand command = new SqlCommand ("Select * from Production.ProductSubCategory as Categories " + " for xml auto,elements", connection); XmlReader reader = command.ExecuteXmlReader(); XPathDocument xpathDoc = new XPathDocument(reader); string xslPath = Server.MapPath("App_Data/Category.xsl"); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(xslPath); XsltArgumentList argsList = new XsltArgumentList(); string backGroundColor = "Tan"; //Add the required parameters to the XsltArgumentList object argsList.AddParam("BackGroundColor", "", backGroundColor); transform.Transform(xpathDoc, argsList, Response.Output); } } </script>
上記のコードは最初のコード例とよく似ていますが、XsltArgumentListオブジェクトが使用されているという点が異なります。具体的には、XsltArgumentListオブジェクトのインスタンスを作成し、そのAddParam()メソッドを呼び出してBackGroundColorパラメータを追加し、最後にXsltArgumentListを引数としてXslCompiledTransformのTransform()メソッドに渡しています。
このように設定して、ブラウザでこのWebページのURLを指定すると、次の画面が表示されます。すべてのカテゴリレコードの背景色が薄茶色になっていることに注目してください。

