XSLT parameters are added to the XsltArgumentList using the AddParam method. A qualified name and namespace URI are associated with the parameter object at that time.
The parameter object should correspond to a W3C type. The following table shows the corresponding W3C types, the equivalent Microsoft .NET classes (type), and whether the W3C type is an XPath type or XSLT type.
*This is equivalent to a node set that contains a single node.
If the parameter object is not one of the above classes, it is converted according to the following rules. Common language runtime (CLR) numeric types are converted to Double. The DateTime type is converted to String. IXPathNavigable types are converted to XPathNavigator. XPathNavigator[] is converted to XPathNodeIterator.
All other types throw an error.
Example
The following example uses the AddParam method to create a parameter to hold calculated discount date. The discount date is calculated to be 20 days from the order date.
C#
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
publicclassSample {
publicstaticvoidMain() {
// Create the XslCompiledTransform and load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("discount.xsl");
// Create the XsltArgumentList.
XsltArgumentList argList = new XsltArgumentList();
// Calculate the discount date.
DateTime orderDate = new DateTime(2004, 01, 15);
DateTime discountDate = orderDate.AddDays(20);
argList.AddParam("discount", "", discountDate.ToString());
// Create an XmlWriter to write the output.
XmlWriter writer = XmlWriter.Create("orderOut.xml");
// Transform the file.
xslt.Transform(new XPathDocument("order.xml"), argList, writer);
writer.Close();
}
}
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 style sheet.
Dim xslt as XslCompiledTransform = new XslCompiledTransform()
xslt.Load("discount.xsl")
' Create the XsltArgumentList.
Dim argList as XsltArgumentList = new XsltArgumentList()
' Calculate the discount date.
Dim orderDate as DateTime = new DateTime(2004, 01, 15)
Dim discountDate as DateTime = orderDate.AddDays(20)
argList.AddParam("discount", "", discountDate.ToString())
' Create an XmlWriter to write the output.
Dim writer as XmlWriter = XmlWriter.Create("orderOut.xml")
' Transform the file.
xslt.Transform(new XPathDocument("order.xml"), argList, writer)
writer.Close()
end sub
end class
Input
order.xml
XML
<!--Represents a customer order--><order><bookISBN='10-861003-324'><title>The Handmaid's Tale</title><price>19.95</price></book><cdISBN='2-3631-4'><title>Americana</title><price>16.95</price></cd></order>
discount.xsl
XML
<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:paramname="discount"/><xsl:templatematch="/"><order><xsl:variablename="sub-total"select="sum(//price)"/><total><xsl:value-ofselect="$sub-total"/></total>
15% discount if paid by: <xsl:value-ofselect="$discount"/></order></xsl:template></xsl:stylesheet>
Output
XML
<?xml version="1.0" encoding="utf-8"?><order><total>36.9</total>
15% discount if paid by: 2/4/2004 12:00:00 AM
</order>
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET
feedback
.NET
is an open source project. Select a link to provide feedback:
This module covers method parameters, including pass-by-reference and pass-by-value parameter types. This module also covers optional and named arguments.