SUMMARY
This step-by-step article describes how to execute a parameterized Extensible Stylesheet Language (XSL) transformation in an ASP.NET application by dynamically supplying values for the input parameters that are defined in the referenced style sheet. You can also use these concepts in other types of .NET applications that execute parameterized XSL transformations.
back to the top
Parameterized XSL Transformations
A parameterized XSLT style sheet is a style sheet that defines and references XSLT parameters to control the execution of the XSL transformation process when it is used to transform data that is contained in a source XML document. Parameters are defined in an XSLT style sheet by using
<xsl:param> elements. The default values for the parameters can be hard-coded in the style sheet by using the
select attribute of the
<xsl:param> element. The more common and practical usage scenario is to dynamically supply the values for the parameters at run time when the transformation is executed programmatically in an XSLT-enabled application. In .NET applications, the
System.Xml.Xsl.XsltArgumentList Framework base class can be used to address this requirement.
back to the top
Create and Set Up the Sample ASP.NET Application
- In Microsoft Visual Studio .NET, create a new Microsoft Visual Basic .NET ASP.NET Web Application project named XSLTParamDemo.
- Add a new XML file named Employees.xml to the project. To do this, follow these steps:
- In the IDE, right-click the project name, and then click Add.
- Click Add New Item, and then click XML File.
- Paste the following code in the file. Use Notepad as an intermediary if you experience any encoding or formatting problems when you try to paste the code in the file:
<?xml version='1.0'?>
<employees>
<employee>
<employeeId>1</employeeId>
<name>
<given>Nancy</given>
<family>Davolio</family>
</name>
</employee>
<employee>
<employeeId>2</employeeId>
<name>
<given>Andrew</given>
<family>Fuller</family>
</name>
</employee>
<employee>
<employeeId>3</employeeId>
<name>
<given>Janet</given>
<family>Leverling</family>
</name>
</employee>
</employees>
- Add a new XSLT file named Sortemployees.xslt to the project. To do this, follow these steps:
- In the IDE, right-click the project name, and then click Add.
- Click Add New Item, and then click XSLT File.
- Paste the following code in the file. Use Notepad as an intermediary if you experience any encoding or formatting problems when you try to paste the code in the file:
This style sheet contains XSLT code to sort and display the data in Employees.xml as an HTML table. XSLT parameters (sortColumn and sortOrder) are used to specify the sort order and column. In this specific sample, the sort column can be either the given name (employee/name/given) or the family name (employee/name/family). The sort order can be either ascending or descending.
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="sortColumn"/>
<xsl:param name="sortOrder"/>
<xsl:template match="employees">
<HTML>
<BODY>
<TABLE BORDER="1">
<TR>
<TD><B>Employees</B></TD>
</TR>
<xsl:apply-templates select="employee/name">
<xsl:sort select="./*[local-name() = $sortColumn]" order="{$sortOrder}"/>
</xsl:apply-templates>
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="employee/name">
<TR>
<TD><xsl:value-of select="family"/>, <xsl:value-of select="given"/></TD>
</TR>
</xsl:template>
</xsl:stylesheet>
- Paste the following code in the Page_Load event procedure of WebForm1.aspx to programmatically execute the XSL transformation by applying Sortemployees.xslt to Employees.xml. Study the inline comments to obtain an understanding of the code and the process that is used to pass in the values for the sortColumn and the sortOrder XSLT parameters:
Try
'Instantiate a new XPathDocument object and load the source XML.
Dim xmlDoc As New System.Xml.XPath.XPathDocument(Server.MapPath("employees.xml"))
'Instantiate a new XslTransform object and load the style sheet.
Dim xslt As New System.Xml.Xsl.XslTransform()
xslt.Load(Server.MapPath("sortemployees.xslt"))
'Instantiate an XsltArgumentList object.
Dim xsltArgList As New System.Xml.Xsl.XsltArgumentList()
'Use the AddParam method to supply the values for the
'sortColumn and the sortOrder parameters defined in the style sheet.
'The second parameter in the AddParam method is used to specify the
'namespace URI to associate with the parameter. A blank string is supplied in this
'example to indicate that the default namespace is to be used.
'The following lines set the values for the sortColumn and the sortOrder
'parameters to sort the data in Employees.xml in descending order on
'the family name.
xsltArgList.AddParam("sortColumn", "", "family")
xsltArgList.AddParam("sortOrder", "", "descending")
'Set the ContentType of the ASP.NET Response object to text/html.
Response.ContentType = "text/html"
'Execute the transformation and generate the output to the Response object's
'output stream.
xslt.Transform(xmlDoc, xsltArgList, Response.OutputStream)
'Exception handling code.
Catch xsltExp As System.Xml.Xsl.XsltException
MsgBox(xsltExp.Message)
Catch xsltCompileExp As System.Xml.Xsl.XsltCompileException
MsgBox(xsltCompileExp.Message)
Catch XPathExp As System.Xml.XPath.XPathException
MsgBox(XPathExp.Message)
Catch XmlExp As System.Xml.XmlException
MsgBox(XmlExp.Message)
End Try
- Save and then build the solution.
back to the top
Test the Sample ASP.NET Application
- Execute the XSLTParamDemo ASP.NET application to start and display Webform1.aspx in the browser. The code in the Page_Load event procedure of Webform1.aspx executes the XSL transformation and generates the output in the form of an HTML table that uses the <lastname>, <firstname> format to display a list of the employee names. The list is sorted in descending order by the family name.
- Close the browser window to quit the ASP.NET application.
- Supply other possible values for the sortColumn and the sortOrder XSLT parameters in the Page_Load() event procedure of WebForm1.aspx to sort the data in the following ways:
- Ascending order by family name
- Ascending order by given name
- Descending order by given name
back to the top
The AddParam Method
The
AddParam method of the
System.Xml.Xsl.XsltArgumentList Framework base class is used in .NET applications to dynamically supply values for XSLT parameters that are defined in parameterized XSLT style sheets when you programmatically execute XSL transformations. The following are the Visual Basic .NET and Microsoft Visual C# .NET syntaxes for this method:
[Visual Basic]
Public Sub AddParam( _
ByVal name As String, _
ByVal namespaceUri As String, _
ByVal parameter As Object _
)
[C#]
public void AddParam(
string name,
string namespaceUri,
object parameter
);
The following is a description of the method parameters:
- name: The name of the parameter as defined in the XSLT style sheet.
- namespaceUri: The namespace Uniform Resource Identifier (URI) to associate with the parameter. To use the default namespace, specify an empty string.
- object: The parameter value or object to add to the list.
back to the top