Passing Parameters into XSLT Document with ASP.NET
XSL documents do a great job styling XML content and doing it quickly, but its not always easy to define parameters within. Say you have a variable within your ASP.NET page that you need to send over to your XSL stylesheet, what do we do? XslTransform and XsltArgumentList are the key. In the code below, we define a new XslTransform then go about setting our XsltArgumentList. The XsltArgumentList takes an array of parameters so feel free to load it up. Once we have all of our parameters defined, we move to loading up our XSLT document via the Load method within XslTransform. The Server.MapPath just traces our path down the directory structure to the document in question. Finally, we need to associate all our content with the asp:Xml control on the page. We load the XML up. In this case, I had it in a string object. The we transform the content with the Xsl document we defined. Next, we associate our parameters we defined.
Now let’s take a look at the XSLT document. We have to define the xsl:param node so we have a placeholder to move our value into. These parameters definitions rest in the header of the document between the xsl:stylesheet and the xsl:template tags. As you can see, we’ve named our variable ‘myVariable’ and given it a default value of ’123′ in case we don’t have a value in which to send over. This default value will be overwritten when we make our call from the ASPX page. Then all we have to do is call our variable using the xsl:value-of select. Be sure to prepend the variable name with the dollar sign so it can properly access the value.
