Recipe15.14.Passing Parameters to XSLT Transformations


Recipe 15.14. Passing Parameters to XSLT Transformations

Problem

You need to use XSLT to produce information that has a few data items that could change between transformations and you don't want to have a separate XSLT stylesheet for each variation.

Solution

Use the XsltArgumentList class to pass arguments to the XSLT transformation. This technique allows the program to generate an object for the stylesheet to access (such as a dynamic string) and use while it transforms the given XML file. The storeTitle and pageDate arguments are passed in to the transformation in the following example. The storeTitle is for the title of the comic store and pageDate is the date the report is run for. These are added using the AddParam method of the XsltArgumentList object instance args.

 XsltArgumentList args = new XsltArgumentList(); args.AddParam("storeTitle", "", "Hero Comics Inventory"); args.AddParam("pageDate", "", DateTime.Now.ToString("F")); // Create a resolver with default credentials. XmlUrlResolver resolver = new XmlUrlResolver(); resolver.Credentials = System.Net.CredentialCache.DefaultCredentials; 

The XsltSettings class allows changing the behavior of the transformation. If you use the XsltSettings.Default instance, the transformation will be done without allowing scripting or the use of the document() XSLT function, as they can be security risks. If the stylesheet is from a trusted source, you can just create an XsltSettings object and use it, but it is better to be safe. Further changes to the code could open it up to use with untrusted XSLT stylesheets.

 XslCompiledTransform transform = new XslCompiledTransform(); // Load up the stylesheet. transform.Load(@"..\..\ParameterExample.xslt", XsltSettings.Default, resolver); // Perform the transformation. FileStream fs = null; using (fs = new FileStream(@"..\..\ParameterExample.htm",      FileMode.OpenOrCreate, FileAccess.Write)) {     transform.Transform(@"..\..\ParameterExample.xml", args, fs); } 

To show the different parameters in action, now you change storeTitle and pageDate again and run the transformation again:

 // Now change the parameters and reprocess. args = new XsltArgumentList(); args.AddParam("storeTitle", "", "Fabulous Adventures Inventory"); args.AddParam("pageDate", "", DateTime.Now.ToString("D")); using (fs = new FileStream(@"..\..\ParameterExample2.htm",     FileMode.OpenOrCreate, FileAccess.Write)) {     transform.Transform(@"..\..\ParameterExample.xml", args, fs); } 

The ParameterExample.xml file contains the following:

 <?xml version="1.0" encoding="utf-8" ?> <ParameterExample>     <ComicBook name="The Amazing Spider-Man" edition="1"/>     <ComicBook name="The Uncanny X-Men" edition="2"/>     <ComicBook name="Superman" edition="3"/>     <ComicBook name="Batman" edition="4"/>     <ComicBook name="The Fantastic Four" edition="5"/> </ParameterExample> 

The ParameterExample.xslt file contains the following:

 <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:output method="html" indent="yes" />    <xsl:param name="storeTitle"/>     <xsl:param name="pageDate"/>     <xsl:template match="ParameterExample">     <html>       <head/>       <body>         <h3><xsl:text>Brought to you by </xsl:text>         <xsl:value-of select="$storeTitle"/><br/>         <xsl:text> on </xsl:text>         <xsl:value-of select="$pageDate"/>         <xsl:text> &#xd;&#xa;</xsl:text>         </h3>         <br/>         <table border="2">           <thead>             <tr>               <td>                 <b>Heroes</b>               </td>               <td>                 <b>Edition</b>               </td>               </tr>           </thead>             <tbody>               <xsl:apply-templates/>             </tbody>         </table>       </body>     </html> </xsl:template>   <xsl:template match="ComicBook">   <tr>     <td>       <xsl:value-of select="@name"/>     </td>     <td>       <xsl:value-of select="@edition"/>     </td>     </tr>   </xsl:template> </xsl:stylesheet> 

The output from the first transformation to ParameterExample.htm is shown in Figure 15-2.

Figure 15-2. Output from the first set of parameters


Output from the second transformation to ParameterExample2.htm is shown in Figure 15-3.

Figure 15-3. Output from the second set of parameters


Discussion

The ability to pass information to the XSLT stylesheet allows a much greater degree of flexibility when designing reports or user interfaces via XSLT transformations. This capability can help customize the output based on just about any criteria you can think of, as the data being passed in is totally controlled by your program. Once you get the hang of using parameters with XSLT, a whole new level of customization becomes possible. As an added bonus, it is portable between environments (.NET, Xalan, etc.).

See Also

See the "XsltArgumentList Class" and "XsltSettings Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net