XSLT パラメーター

XSLT パラメーターを XsltArgumentList に追加するには、AddParam メソッドを使用します。 その時点で、修飾名と名前空間 URI がそのパラメーター オブジェクトに関連付けられます。

XSLT パラメーターを使用するために必要な処理

  1. XsltArgumentList オブジェクトを作成し、AddParam メソッドを使用してパラメーターを追加します。

  2. スタイル シートからパラメーターを呼び出します。

  3. XsltArgumentList オブジェクトを Transform メソッドに渡します。

パラメーターの型

このパラメーター オブジェクトは、W3C 型に対応している必要があります。 対応する W3C 型、これと同等の Microsoft .NET のクラス (型)、および W3C 型が XPath 型か XSLT 型であるかを次の表に示します。

W3C 型 対応する .NET クラス (型) XPath 型または XSLT 型
String System.String XPath
Boolean System.Boolean XPath
Number System.Double XPath
Result Tree Fragment System.Xml.XPath.XPathNavigator XSLT
Node* System.Xml.XPath.XPathNavigator XPath
Node Set XPathNodeIterator

XPathNavigator[]
XPath

\* これは単一のノードを含むノード セットに相当します。

パラメーター オブジェクトが上記のクラスのいずれでもない場合、次の規則に従って型が変換されます。 共通言語ランタイム (CLR) の数値型は、Double に変換されます。 DateTime 型は String に変換されます。 IXPathNavigable 型は XPathNavigator に変換されます。 XPathNavigator[]XPathNodeIterator に変換されます。

その他の型はエラーになります。

算出された割引日を保持するパラメーターを AddParam メソッドを使用して作成する例を次に示します。 割引日は、発注日から 20 日後として算出されます。

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public class Sample {

   public static void Main() {

      // Create the XslCompiledTransform and load the style sheet.
      XslCompiledTransform xslt = new XslCompiledTransform();
      xslt.Load("discount.xsl");

      // Create the XsltArgumentList.
      XsltArgumentList argList = new XsltArgumentList();

      // Calculate the discount date.
      DateTime orderDate = new DateTime(2004, 01, 15);
      DateTime discountDate = orderDate.AddDays(20);
      argList.AddParam("discount", "", discountDate.ToString());

      // Create an XmlWriter to write the output.
     XmlWriter writer = XmlWriter.Create("orderOut.xml");

     // Transform the file.
     xslt.Transform(new XPathDocument("order.xml"), argList, writer);
     writer.Close();
  }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl

public class Sample

    public shared sub Main()

        ' Create the XslCompiledTransform and load the style sheet.
        Dim xslt as XslCompiledTransform = new XslCompiledTransform()
        xslt.Load("discount.xsl")

        ' Create the XsltArgumentList.
        Dim argList as XsltArgumentList = new XsltArgumentList()

        ' Calculate the discount date.
        Dim orderDate as DateTime = new DateTime(2004, 01, 15)
        Dim discountDate as DateTime = orderDate.AddDays(20)
        argList.AddParam("discount", "", discountDate.ToString())

        ' Create an XmlWriter to write the output.             
        Dim writer as XmlWriter = XmlWriter.Create("orderOut.xml")

        ' Transform the file.
        xslt.Transform(new XPathDocument("order.xml"), argList, writer)
        writer.Close()

    end sub

end class

入力

order.xml
<!--Represents a customer order-->
<order>
  <book ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <cd ISBN='2-3631-4'>
    <title>Americana</title>
    <price>16.95</price>
  </cd>
</order>
discount.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="discount"/>
  <xsl:template match="/">
    <order>
      <xsl:variable name="sub-total" select="sum(//price)"/>
      <total><xsl:value-of select="$sub-total"/></total>
           15% discount if paid by: <xsl:value-of select="$discount"/>
     </order>
  </xsl:template>
</xsl:stylesheet>

Output

<?xml version="1.0" encoding="utf-8"?>  
<order>  
  <total>36.9</total>  
     15% discount if paid by: 2/4/2004 12:00:00 AM  
</order>  

関連項目