Share via

XsltArgumentList.AddExtensionObject(String, Object) Method


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

 void AddExtensionObject(System::String ^ namespaceUri, System::Object ^ extension);
public void AddExtensionObject (string namespaceUri, object extension);
member this.AddExtensionObject : string * obj -> unit
Public Sub AddExtensionObject (namespaceUri As String, extension As Object)



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


The object to add to the list.


The namespaceUri is either null or http://www.w3.org/1999/XSL/Transform

The namespaceUri already has an extension object associated with it.

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


In the following example, the style sheet uses an XSLT extension object to convert the book price.

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 stylesheet.
    XslCompiledTransform xslt = new XslCompiledTransform();

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

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

    using (XmlWriter w = XmlWriter.Create("output.xml"))
        // Transform the file.
        xslt.Transform("books.xml", xslArg, w);

  // 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;
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 stylesheet.
        Dim xslt As New XslCompiledTransform()
        ' Create an XsltArgumentList.
        Dim xslArg As New XsltArgumentList()
        ' Add an object to calculate the new book price.
        Dim obj As New BookPrice()
        xslArg.AddExtensionObject("urn:price-conv", obj)
        Using w As XmlWriter = XmlWriter.Create("output.xml")
            ' Transform the file.
            xslt.Transform("books.xml", xslArg, w)
        End Using

    End Sub
    ' Convert the book price to a new price using the conversion factor.    
    Public Class BookPrice
        Private newprice As Decimal = 0
        Public Function NewPriceFunc(ByVal price As Decimal, ByVal conv As Decimal) As Decimal 
            Dim tmp As Decimal = price * conv
            newprice = Decimal.Round(tmp, 2)
            Return newprice        
        End Function 'NewPriceFunc

    End Class

End Class

The example uses the following data files as input.


  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>


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

<!--Price conversion factor-->
<xsl:param name="conv" select="1.15"/>

  <xsl:template match="bookstore">
  <xsl:for-each select="book">
    <xsl:copy-of select="node()"/>
          <xsl:value-of select="myObj:NewPriceFunc(./price, $conv)"/>        


The params keyword, which allows an unspecified number of parameters to be passed, is currently not supported. XSLT style sheets that utilize methods defined with the params keyword does not work correctly. For more information, see params.

Applies to

See also