.NET Framework Class Library
XsltArgumentList..::.AddExtensionObject Method

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

Namespace:  System.Xml.Xsl
Assembly:  System.Xml (in System.Xml.dll)
Syntax

Visual Basic (Declaration)
Public Sub AddExtensionObject ( _
    namespaceUri As String, _
    extension As Object _
)
Visual Basic (Usage)
Dim instance As XsltArgumentList
Dim namespaceUri As String
Dim extension As Object

instance.AddExtensionObject(namespaceUri, _
    extension)
C#
public void AddExtensionObject(
    string namespaceUri,
    Object extension
)
Visual C++
public:
void AddExtensionObject(
    String^ namespaceUri, 
    Object^ extension
)
JScript
public function AddExtensionObject(
    namespaceUri : String, 
    extension : Object
)

Parameters

namespaceUri
Type: System..::.String
The namespace URI to associate with the object. To use the default namespace, specify an empty string.
extension
Type: System..::.Object
The object to add to the list.
Exceptions

ExceptionCondition
ArgumentException

The namespaceUri is either nullNothingnullptra 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

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 (C# Reference).

Examples

In the following example, the style sheet 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

    Public Shared Sub Main() 

        ' Create the XslCompiledTransform and load the stylesheet.
        Dim xslt As New XslCompiledTransform()
        xslt.Load("prices.xsl")

        ' 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 'Main 

    ' 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 'BookPrice

End Class 'Sample
C#
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();
    xslt.Load("prices.xsl");

    // 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;
    }
  }
}
CPP_OLD
#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();
}

The example uses the following data files as input.

books.xml

None
<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

None
<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">
  <bookstore>
  <xsl:for-each select="book">
    <book>
    <xsl:copy-of select="node()"/>
       <new-price>
          <xsl:value-of select="myObj:NewPriceFunc(./price, $conv)"/>        
       </new-price>
    </book>
  </xsl:for-each>
  </bookstore>
  </xsl:template>
</xsl:stylesheet>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker