次の方法で共有


XsltArgumentList.AddParam メソッド

パラメータを XsltArgumentList に追加し、それを名前空間限定名に関連付けます。

Public Sub AddParam( _
   ByVal name As String, _   ByVal namespaceUri As String, _   ByVal parameter As Object _)
[C#]
public void AddParam(stringname,stringnamespaceUri,objectparameter);
[C++]
public: void AddParam(String* name,String* namespaceUri,Object* parameter);
[JScript]
public function AddParam(
   name : String,namespaceUri : String,parameter : Object);

パラメータ

  • name
    パラメータに関連付ける名前。
  • namespaceUri
    パラメータに関連付ける名前空間 URI。既定の名前空間を使用するには、空の文字列を指定します。
  • parameter
    リストに追加するパラメータ値またはオブジェクト。

例外

例外の種類 条件
ArgumentException namespaceUri が null 参照 (Visual Basic では Nothing) または http://www.w3.org/1999/XSL/Transform です。

name が、W3C XML 仕様に準拠した有効な名前ではありません。

namespaceUri には、既にパラメータが関連付けられています。

解説

parameter は、下のリストに示す W3C 仕様の型の 1 つである必要があります。W3C 仕様の型 (XPath または XSLT のいずれか)、および対応する .NET クラスを次の表に示します。

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

parameter が前のクラスの 1 つでない場合は、適宜 Double または String に強制的に変換されます。 Int16UInt16Int32UInt32Int64UInt64SingleDecimal の各データ型は、強制的に Double に変換されます。それ以外の型はすべて、 ToString メソッドを使用して、強制的に String に変換されます。

使用例

[Visual Basic, C#, C++] AddParam メソッドを使用して、現在の日付と時刻を表すパラメータを作成する例を次に示します。

 
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl

public class Sample

   private shared filename as String = "order.xml"
   private shared stylesheet as String = "order.xsl"

   public shared sub Main() 
    
      'Create the XslTransform and load the stylesheet.
      Dim xslt as XslTransform = new XslTransform()
      xslt.Load(stylesheet)

      'Create the XsltArgumentList.
      Dim xslArg as XsltArgumentList = new XsltArgumentList()
         
      'Create a parameter which represents the current date and time.
      Dim d as DateTime = DateTime.Now
      xslArg.AddParam("date", "", d.ToString())

      'Create the XmlTextWriter to write the output to the console.             
      Dim writer as XmlTextWriter = new XmlTextWriter(Console.Out)

     'Transform the file.
     xslt.Transform(new XPathDocument(filename), xslArg, writer, nothing)
     writer.Close()

  end sub
end class

[C#] 
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;


public class Sample
{
   private const String filename = "order.xml";
   private const String stylesheet = "order.xsl";

   public static void Main() 
   {
    
      //Create the XslTransform and load the stylesheet.
      XslTransform xslt = new XslTransform();
      xslt.Load(stylesheet);

      //Create the XsltArgumentList.
      XsltArgumentList xslArg = new XsltArgumentList();
         
      //Create a parameter which represents the current date and time.
      DateTime d = DateTime.Now;
      xslArg.AddParam("date", "", d.ToString());

      //Create the XmlTextWriter to write the output to the console.             
     XmlTextWriter writer = new XmlTextWriter(Console.Out);

     //Transform the file.
     xslt.Transform(new XPathDocument(filename), xslArg, writer, null);
     writer.Close();

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::XPath;
using namespace System::Xml::Xsl;

int main()
{
   String * filename = S"order.xml";
   String * stylesheet = S"order.xsl";

   // Create the XslTransform and load the stylesheet.
   XslTransform* xslt = new XslTransform();
   xslt -> Load(stylesheet);

   // Create the XsltArgumentList.
   XsltArgumentList* xslArg = new XsltArgumentList();

   // Create a parameter which represents the current date and time.
   DateTime d = DateTime::Now;
   xslArg -> AddParam(S"date", S"", __box(d));

   // Create the XmlTextWriter to write the output to the console.             
   XmlTextWriter* writer = new XmlTextWriter(Console::Out);

   // Transform the file.
   xslt -> Transform(new XPathDocument(filename), xslArg, writer, 0);
   writer -> Close();
}

[Visual Basic, C#, C++] この例では、次の 2 つのデータ ファイルを入力として使用しています。

[Visual Basic, C#, C++] 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>

order.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="date"/>
  <xsl:template match="/">
    <order>
      <date><xsl:value-of select="$date"/></date>
      <total><xsl:value-of select="sum(//price)"/></total>
    </order>
  </xsl:template>
</xsl:stylesheet>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XsltArgumentList クラス | XsltArgumentList メンバ | System.Xml.Xsl 名前空間