Microsoft KB Archive/321704

= HOW TO: Execute Parameterized XSL Transformations in .NET Applications =

Article ID: 321704

Article Last Modified on 1/22/2004

-

APPLIES TO


 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 1.1

-



This article was previously published under Q321704



IN THIS TASK

 * SUMMARY


 * Parameterized XSL Transformations
 * Create and Set Up the Sample ASP.NET Application
 * Test the Sample ASP.NET Application
 * The AddParam Method



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  elements. The default values for the parameters can be hard-coded in the style sheet by using the select attribute of the  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:</ol>

<?xml version='1.0'?> <employeeId>1</employeeId> Nancy Davolio <employeeId>2</employeeId> Andrew Fuller <employeeId>3</employeeId> Janet Leverling </li>  Add a new XSLT file named Sortemployees.xslt to the project. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> In the IDE, right-click the project name, and then click Add.</li> Click Add New Item, and then click XSLT File.</li> 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:</li></ol>

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=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;> <xsl:param name=&quot;sortColumn&quot;/> <xsl:param name=&quot;sortOrder&quot;/>

<xsl:template match=&quot;employees&quot;> <HTML> <BODY>

</BODY> </HTML> </xsl:template>

<xsl:template match=&quot;employee/name&quot;> <TR> <TD><xsl:value-of select=&quot;family&quot;/>, <xsl:value-of select=&quot;given&quot;/></TD> </TR> </xsl:template>

</xsl:stylesheet> </li>  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(&quot;employees.xml&quot;))

'Instantiate a new XslTransform object and load the style sheet. Dim xslt As New System.Xml.Xsl.XslTransform xslt.Load(Server.MapPath(&quot;sortemployees.xslt&quot;))

'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(&quot;sortColumn&quot;, &quot;&quot;, &quot;family&quot;) xsltArgList.AddParam(&quot;sortOrder&quot;, &quot;&quot;, &quot;descending&quot;)

'Set the ContentType of the ASP.NET Response object to text/html. Response.ContentType = &quot;text/html&quot;

'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 </li> Save and then build the solution.</li></ol>

back to the top

Test the Sample ASP.NET Application

 * 1) 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 ,  format to display a list of the employee names. The list is sorted in descending order by the family name.
 * 2) Close the browser window to quit the ASP.NET application.
 * 3) 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:
 * 4) * Ascending order by family name
 * 5) * Ascending order by given name
 * 6) * 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

Keywords: kbhowtomaster kbbcl KB321704

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.