XSLT extension objects are added to the XsltArgumentList using the AddExtensionObject 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 AddExtensionObject 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 Transform method is called, permissions are calculated against the evidence provided at Load 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:
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>