| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
XsltArgumentList for Style Sheet Parameters and Extension Objects |
03/30/2017 |
.net |
dotnet-standard |
article |
|
de2f0dce-6b98-4908-bba7-ed150cc50355 |
3 |
mairaw |
mairaw |
wpickett |
XsltArgumentList for Style Sheet Parameters and Extension Objects
The xref:System.Xml.Xsl.XsltArgumentList class contains Extensible Stylesheet Language for Transformations (XSLT) parameters and XSLT extension objects. When passed into the xref:System.Xml.Xsl.XslTransform.Transform%2A method, these parameters and extension objects can be invoked from style sheets.
[!NOTE] The xref:System.Xml.Xsl.XslTransform and xref:System.Xml.Xsl.XsltArgumentList classes are obsolete in the [!INCLUDEdnprdnext]. You can perform XSLT transformations using the xref:System.Xml.Xsl.XslCompiledTransform class. See Using the XslCompiledTransform Class and Migrating From the XslTransform Class for more information.
The xref:System.Xml.Xsl.XsltArgumentList class contains XSLT parameters and XSLT extension objects. When passed into the xref:System.Xml.Xsl.XslTransform.Transform%2A method, these parameters and extension objects can be invoked from style sheets.
The following are advantages to passing an object rather than using an embedded script:
-
Provides better encapsulation and reuse of classes.
-
Allows style sheets to be smaller and more maintainable.
-
Supports calling methods on classes belonging to namespaces other than those defined within the set of supported xref:System namespaces.
-
Supports passing result tree fragments to the style sheet with the use of the xref:System.Xml.XPath.XPathNodeIterator.
XSLT Style Sheet Parameters
XSLT parameters are added to the xref:System.Xml.Xsl.XsltArgumentList using the xref:System.Xml.Xsl.XsltArgumentList.AddParam%2A method. A qualified name and namespace Uniform Resource Identifier (URI) are associated with the parameter object at that time.
The parameter object should correspond to a World Wide Web Consortium (W3C) type. The following table shows the corresponding W3C types, the equivalent [!INCLUDEdnprdnshort] classes (type), and whether the W3C type is an XML Path Language (XPath) type or XSLT type.
| W3C Type | Equivalent .NET Framework class (type) | XPath type or XSLT type |
|---|---|---|
| String | System.String | XPath |
| Boolean | System.Boolean | XPath |
| Number | System.Double | XPath |
| Result Tree Fragment | System.Xml.XPath.XPathNavigator | XSLT |
| Node Set | System.Xml.XPath.XPathNodeIterator | XPath |
If the parameter object is not one of the above classes, it is forced to either a Double or String, as appropriate. Int16, UInt16, Int32, UInt32, Int64, UInt64, Single and Decimal types are forced to a Double. All other types are forced to a String using the ToString method.
To use the XSLT parameter, the user needs to do the following:
-
Create an xref:System.Xml.Xsl.XsltArgumentList and add the objects using xref:System.Xml.Xsl.XsltArgumentList.AddParam%2A.
-
Call the parameters from the style sheet.
-
Pass the xref:System.Xml.Xsl.XsltArgumentList to the xref:System.Xml.Xsl.XslTransform.Transform%2A method.
Example
The following example uses the xref:System.Xml.Xsl.XsltArgumentList.AddParam%2A method to create a parameter to hold a calculated discount date. The discount date is calculated to be 20 days from the order date.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Public class Sample
Private Const filename As String = "order.xml"
Private Const stylesheet As String = "discount.xsl"
Public Shared Sub Main()
'Create the XslTransform and load the style sheet.
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
'Calculate the discount date.
Dim today As DateTime = DateTime.Now
Dim d As DateTime = today.AddDays(20)
xslArg.AddParam("discount", "", d.ToString())
'Create an XmlTextWriter to handle the output.
Dim writer As XmlTextWriter = New XmlTextWriter("orderout.xml", Nothing)
'Transform the file.
xslt.Transform(doc, xslArg, writer, Nothing)
writer.Close()
End Sub
End Class 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 = "discount.xsl";
public static void Main() {
//Create the XslTransform and load the style sheet.
XslTransform xslt = new XslTransform();
xslt.Load(stylesheet);
//Load the XML data file.
XPathDocument doc = new XPathDocument(filename);
//Create an XsltArgumentList.
XsltArgumentList xslArg = new XsltArgumentList();
//Calculate the discount date.
DateTime today = DateTime.Now;
DateTime d = today.AddDays(20);
xslArg.AddParam("discount", "", d.ToString());
//Create an XmlTextWriter to handle the output.
XmlTextWriter writer = new XmlTextWriter("orderout.xml", null);
//Transform the file.
xslt.Transform(doc, xslArg, writer, null);
writer.Close();
}
} 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> discount.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="discount"/>
<xsl:template match="/">
<order>
<xsl:variable name="sub-total" select="sum(//price)"/>
<total><xsl:value-of select="$sub-total"/></total>
15% discount if paid by: <xsl:value-of select="$discount"/>
</order>
</xsl:template>
</xsl:stylesheet> Output
<order>
<total>36.9</total>
15% discount if paid by: 5/6/2001 5:01:15 PM
</order> XSLT Extension Objects
XSLT extension objects are added to the xref:System.Xml.Xsl.XsltArgumentList using the xref:System.Xml.Xsl.XsltArgumentList.AddExtensionObject%2A method. A qualified name and namespace URI are associated with the extension object at that time.
When an object is added, the caller of the xref:System.Xml.Xsl.XsltArgumentList.AddExtensionObject%2A must be fully trusted in the security policy. If the caller is semi-trusted, the addition will fail.
Though an object is added successfully, it does not guarantee that the execution will be successful. When the xref:System.Xml.Xsl.XslTransform.Transform%2A method is called, permissions are calculated against the evidence provided at xref:System.Xml.Xsl.XslTransform.Load%2A time, and that permission set is assigned to the entire transformation process. If an extension object attempts to initiate an action that requires permissions not found in the set, an exception is thrown.
The data types returned from extension objects are one of the four basic XPath data types of number, string, Boolean, and node set.
To use the XSLT extension object, the user needs to do the following:
-
Create an xref:System.Xml.Xsl.XsltArgumentList and add the extension object using xref:System.Xml.Xsl.XsltArgumentList.AddExtensionObject%2A.
-
Invoke the extension object from the style sheet.
-
Pass the xref:System.Xml.Xsl.XsltArgumentList to the xref:System.Xml.Xsl.XslTransform.Transform%2A method.
Example
The following example calculates the circumference of a circle given its radius.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Public Class Sample
Private Const filename As String = "number.xml"
Private Const stylesheet As String = "circle.xsl"
Public Shared Sub Main()
Dim test As Sample = New Sample
End Sub
Public Sub New()
'Create the XslTransform and load the style sheet.
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 calculate the circumference of the circle.
Dim obj As Calculate = New Calculate
xslArg.AddExtensionObject("urn:myObj", 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
'Calculates the circumference of a circle given the radius.
Public Class Calculate
Private circ As double = 0
Public Function Circumference(radius As Double) As Double
circ = Math.PI*2*radius
Return circ
End Function
End Class
End Class using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public class Sample
{
private const String filename = "number.xml";
private const String stylesheet = "circle.xsl";
public static void Main() {
Sample test = new Sample();
}
public Sample() {
//Create the XslTransform and load the style sheet.
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 calculate the circumference of the circle.
Calculate obj = new Calculate();
xslArg.AddExtensionObject("urn:myObj", 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();
}
//Calculates the circumference of a circle given the radius.
public class Calculate{
private double circ = 0;
public double Circumference(double radius){
circ = Math.PI*2*radius;
return circ;
}
}
} Input
number.xml
<?xml version='1.0'?>
<data>
<circle>
<radius>12</radius>
</circle>
<circle>
<radius>37.5</radius>
</circle>
</data> circle.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myObj="urn:myObj">
<xsl:template match="data">
<circles>
<xsl:for-each select="circle">
<circle>
<xsl:copy-of select="node()"/>
<circumference>
<xsl:value-of select="myObj:Circumference(radius)"/>
</circumference>
</circle>
</xsl:for-each>
</circles>
</xsl:template>
</xsl:stylesheet> Output
<circles xmlns:myObj="urn:myObj">
<circle>
<radius>12</radius>
<circumference>75.398223686155</circumference>
</circle>
<circle>
<radius>37.5</radius>
<circumference>235.61944901923448</circumference>
</circle>
</circles>