次の方法で共有


XsltArgumentList.AddExtensionObject メソッド

新しいオブジェクトを XsltArgumentList に追加し、それを名前空間 URI に関連付けます。

呼び出し時の注意: .NET Framework Version 1.1 でこのメソッドを呼び出すには FullTrust が必要です。詳細については、「 コード アクセス セキュリティ 」を参照してください。

Public Sub AddExtensionObject( _
   ByVal namespaceUri As String, _   ByVal extension As Object _)
[C#]
public void AddExtensionObject(stringnamespaceUri,objectextension);
[C++]
public: void AddExtensionObject(String* namespaceUri,Object* extension);
[JScript]
public function AddExtensionObject(
   namespaceUri : String,extension : Object);

パラメータ

  • namespaceUri
    オブジェクトに関連付ける名前空間 URI。既定の名前空間を使用するには、空の文字列を指定します。
  • extension
    リストに追加するオブジェクト。

例外

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

namespaceUri には、既に拡張オブジェクトが関連付けられています。

SecurityException 呼び出し元に、このメソッドを呼び出すための十分なアクセス許可がありません。

解説

params キーワードを使用すると、数を指定せずにパラメータを渡すことができますが、このキーワードで定義されるメソッドは現在 XsltArgumentList クラスではサポートされていません。この params キーワードを使用して定義されるメソッドを利用する XSLT スタイルシートは正しく動作しません。詳細については、 params のトピックを参照してください。

使用例

[Visual Basic, C#, C++] スタイル シートで XSLT 拡張オブジェクトを使用し、書籍の価格を変換する例を次に示します。

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


public class Sample

   private shared filename as String = "books.xml"
   private shared stylesheet as String = "prices.xsl"

   public shared sub Main() 
        Dim test as Sample = new Sample()
   end sub
    
  public sub Sample() 

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

    'Load the XML data file.
    Dim doc as XPathDocument = new XPathDocument(filename)

    'Create an XsltArgumentList.
    Dim xslArg as XsltArgumentList = new XsltArgumentList()
         
    'Add an object to convert the book price.
    Dim obj as BookPrice = new BookPrice()
    xslArg.AddExtensionObject("urn:price-conv", obj)

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

    'Transform the file.
    xslt.Transform(doc, xslArg, writer, nothing)
    writer.Close()

  end sub

  'Convert the book price to a new price Imports the conversion factor.
  public class BookPrice

    private newprice as decimal = 0
        
    public function NewPriceFunc(price as decimal, conv as decimal) as decimal
       Dim tmp as decimal = price*conv
       Dim newprice as decimal = decimal.Round(tmp, 2)
       'return newprice
    end function
    
  end class
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 = "books.xml";
   private const String stylesheet = "prices.xsl";

   public static void Main() {

        Sample test = new Sample();
    }
    
  public Sample() {

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

    //Load the XML data file.
    XPathDocument doc = new XPathDocument(filename);

    //Create an XsltArgumentList.
    XsltArgumentList xslArg = new XsltArgumentList();
         
    //Add an object to convert the book price.
    BookPrice obj = new BookPrice();
    xslArg.AddExtensionObject("urn:price-conv", obj);

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

    //Transform the file.
    xslt.Transform(doc, xslArg, writer, null);
    writer.Close();

  }

  //Convert the book price to a new price using the conversion factor.
  public class BookPrice{

    private decimal newprice = 0;
        
    public decimal NewPriceFunc(decimal price, decimal conv){
       decimal tmp = price*conv;
       newprice = decimal.Round(tmp, 2);
       return newprice;
    }
  }
}

[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;

// Convert the book price to a new price using the conversion factor.
__gc public class BookPrice
{
private:
   Decimal newprice;

public:
   BookPrice()
   {
      newprice = 0;
   }

   Decimal NewPriceFunc(Decimal price, Decimal conv)
   {
      Decimal tmp = price*conv;
      newprice = Decimal::Round(tmp, 2);
      return newprice;
   }
};

__gc public class Sample
{
private:
   String * filename;
   String * stylesheet;

public:
   Sample()
   {
      filename =  S"books.xml";
      stylesheet = S"prices.xsl";
      // Create the XslTransform and load the stylesheet.
      XslTransform* xslt = new XslTransform();
      xslt -> Load(stylesheet);

      // Load the XML data file.
      XPathDocument* doc = new XPathDocument(filename);

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

      // Add an object to convert the book price.
      BookPrice* obj = new BookPrice();
      xslArg -> AddExtensionObject(S"urn:price-conv", obj);

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

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

int main()
{
   Sample* test = new Sample();
}

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

[Visual Basic, C#, C++] books.xml

<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

prices.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myObj="urn:price-conv">

<!--currency conversion factor-->
<xsl:param name="conv" select="1.537"/>

  <xsl:template match="bookstore">
  <bookstore>
  <xsl:for-each select="book">
    <book>
    <xsl:copy-of select="node()"/>
       <conv-price>
          <!--<xsl:value-of select="myObj:NewPriceFunc(./price, $conv)"/>-->        
       </conv-price>
    </book>
  </xsl:for-each>
  </bookstore>
  </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 名前空間