Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 1.1
.NET Framework
Reference
System.Xml.Xsl
Methods
 AddExtensionObject Method
This page is specific to
Microsoft Visual Studio 2003/.NET Framework 1.1

Other versions are also available for the following:
.NET Framework Class Library
XsltArgumentList.AddExtensionObject Method

Adds a new object to the XsltArgumentList and associates it with the namespace URI.

Notes to Callers:  In version 1.1 of the .NET Framework, FullTrust is required to call this method. See Code Access Security for more information.

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

Parameters

namespaceUri
The namespace URI to associate with the object. To use the default namespace, specify an empty string.
extension
The object to add to the list.

Exceptions

Exception Type Condition
ArgumentException The namespaceUri is either a null reference (Nothing in Visual Basic) or http://www.w3.org/1999/XSL/Transform

The namespaceUri already has an extension object associated with it.

SecurityException The caller does not have sufficient permissions to call this method.

Remarks

Methods that are defined with the params keyword, which allows an unspecified number of parameters to be passed, is not currently supported by the XsltArgumentList class. XSLT stylesheets that utilize methods defined with the params keyword will not work correctly. See params for more details.

Example

[Visual Basic, C#, C++] In the following example, the stylesheet uses an XSLT extension object to convert the book price.

[Visual Basic] 
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++] The example uses the following data files as input.

[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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XsltArgumentList Class | XsltArgumentList Members | System.Xml.Xsl Namespace

© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker