System.Xml.Xsl.XsltArgumentList Class

Assembly: System.Xml.dll
Namespace: System.Xml.Xsl
Summary
Contains a variable number of arguments which are either XSLT parameters or extension objects.
C# Syntax:
public sealed class XsltArgumentList
Remarks
This class is used by the XslTransform.Transform method. It allows parameters and extension objects to be invoked from within the stylesheet.

When the parameters and objects are added to the XsltArgumentList, they are associated with a namespace qualified name and a namespace URI, respectively.

The following are advantages to passing an object rather than using an embedded script such as <msxsl:script> :

For more information about using the XsltArgumentList, see the conceptual topic at MSDN: xsltargumentlistforstylesheetparametersextensionobjects.

See also:
System.Xml.Xsl Namespace

System.Xml.Xsl.XsltArgumentList Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Public Methods
AddExtensionObject Adds a new object to the XsltArgumentList and associates it with the namespace URI.
AddParam Adds a parameter to the XsltArgumentList and associates it with the namespace qualified name.
Clear Removes all parameters and extension objects from the XsltArgumentList.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetExtensionObject Gets the object associated with the given namespace.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetParam Gets the parameter associated with the namespace qualified name.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
RemoveExtensionObject Removes the object with the namespace URI from the XsltArgumentList.
RemoveParam Removes the parameter from the XsltArgumentList.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.Xsl.XsltArgumentList Member Details

ctor #1
Summary
Implements a new instance of the XsltArgumentList.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XsltArgumentList();

Return to top


Method: AddExtensionObject(
   string namespaceUri,
   object extension
)
Summary
Adds a new object to the XsltArgumentList and associates it with the namespace URI.
C# Syntax:
public void AddExtensionObject(
   string namespaceUri,
   object extension
);
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 null or http://www.w3.org/1999/XSL/Transform

The namespaceUri already has an extension object associated with it.

Example
In the following example, the stylesheet 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
{
   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);
    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;
    }
  }
}

    

The example uses the following data files as input.

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>

    

Return to top


Method: AddParam(
   string name,
   string namespaceUri,
   object parameter
)
Summary
Adds a parameter to the XsltArgumentList and associates it with the namespace qualified name.
C# Syntax:
public void AddParam(
   string name,
   string namespaceUri,
   object parameter
);
Parameters:

name

The name to associate with the parameter.

namespaceUri

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

parameter

The parameter value or object to add to the list.

Exceptions
Exception Type Condition
ArgumentException The namespaceUri is either null or http://www.w3.org/1999/XSL/Transform .

The name is not a valid name according to the W3C XML specification.

The namespaceUri already has a parameter associated with it.

Remarks
The parameter should be one of the W3C XML Path Language (XPath) types. The following table shows the W3C XPath types and the corresponding .NET class.

W3C XPath Type Equivalent .NET XPath Class (Type)
String System.String
Boolean System.Boolean
Number System.Double
Node Fragment System.Xml.XPath.XPathNavigator
Node Set System.Xml.XPath.XPathNodeIterator

If parameter is not one of the preceding classes, it is coerced to either a Double or String, as appropriate.Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, and Decima l types are coerced to a Double. All other types are coerced to a String using the ToString method.

Example
The following example uses the AddParam method to create a parameter representing the current date and time.
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);
     writer.Close();

  }
}

    

The example uses the following two data files as input.

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>

    

Return to top


Method: Clear()
Summary
Removes all parameters and extension objects from the XsltArgumentList.
C# Syntax:
public void Clear();

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XsltArgumentList();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetExtensionObject(
   string namespaceUri
)
Summary
Gets the object associated with the given namespace.
C# Syntax:
public object GetExtensionObject(
   string namespaceUri
);
Parameters:

namespaceUri

The namespace URI of the object.

Return Value:
The namespace URI object or null if one was not found.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetParam(
   string name,
   string namespaceUri
)
Summary
Gets the parameter associated with the namespace qualified name.
C# Syntax:
public object GetParam(
   string name,
   string namespaceUri
);
Parameters:

name

The name of the parameter.XsltArgumentList does not check to ensure the name passed is a valid local name; however, the name cannot be null.

namespaceUri

The namespace URI associated with the parameter.

Return Value:
The parameter object or null if one was not found.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: RemoveExtensionObject(
   string namespaceUri
)
Summary
Removes the object with the namespace URI from the XsltArgumentList.
C# Syntax:
public object RemoveExtensionObject(
   string namespaceUri
);
Parameters:

namespaceUri

The namespace URI associated with the object to remove.

Return Value:
The object with the namespace URI or null if one was not found.

Return to top


Method: RemoveParam(
   string name,
   string namespaceUri
)
Summary
Removes the parameter from the XsltArgumentList.
C# Syntax:
public object RemoveParam(
   string name,
   string namespaceUri
);
Parameters:

name

The name of the parameter to remove.XsltArgumentList does not check to ensure the name passed is a valid local name; however, the name cannot be null.

namespaceUri

The namespace URI of the parameter to remove.

Return Value:
The parameter object or null if one was not found.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.