XML (.NET 1.1) Performance Guidelines - XSLT Processing

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman


Contents

Use XPathDocument for Faster XSLT Transformations

The XPathDocument class provides a fast, read-only cache for XML document processing by using XSLT. Use this class for optimum performance. The following code fragment shows how to use this class.


  XslTransform xslt = new XslTransform();
  xslt.Load(someStylesheet);
  XPathDocument doc = new XPathDocument("books.xml");
  StringWriter fs = new StringWriter();
  xslt.Transform(doc, null, fs, null);


Consider Caching Compiled Style Sheets

If your application performs a common transformation by using the same style sheet on a per-request basis, consider caching the style sheet between requests. This is a strong recommendation because it saves you having to recompile the style sheet every time you perform a transformation. In a .NET application, you compile the .NET application once into an executable file and then run it many times. The same applies to XSLT.

The following code fragment shows the XslTransform class being cached in the ASP.NET application state. Note that the XslTransform class is thread-safe.


  protected void Application_Start(Object sender, EventArgs e)
  {
    //Create the XslTransform and load the style sheet.
    XslTransform xslt = new XslTransform();
    xslt.Load(stylesheet);
    //Save it to ASP.NET application scope
    Application["XSLT"] = xslt;
  }
  private void Page_Load(object sender, System.EventArgs e)
  {
    // Re-use the XslTransform stored in the application scope
    XslTransform xslt = Application["XSLT"];
  }


Caching Extension Objects

You can use Extension objects to implement custom functions that are referenced in XPath query expressions that are used in an XSLT style sheet. The XSLT processor does not automatically cache Extension objects. However, you can cache XsltArgumentList objects that are used to supply Extension objects. This approach is shown in the following code fragment.


  // 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 xslArgCache = new XsltArgumentList();
  // Add an object to calculate the circumference of the circle.
  Calculate obj = new Calculate();
  xslArgCache.AddExtensionObject("urn:myObj", obj);
  // Create an XmlTextWriter to output to the console.
  XmlTextWriter writer = new XmlTextWriter(Console.Out);
  // Transform the file.
  xslt.Transform(doc, xslArgCache, writer);
  writer.Close();
  // Reuse xslArgCache
  ……..
  xslt.Transform(doc2, xslArgCache, writer2);


Split Complex Transformations into Several Stages

You can incrementally transform an XML document by using multiple XSLT style sheets to generate the final required output. This process is referred to as pipelining and is particularly beneficial for complex transformations over large XML documents.


References

For more information about how to split complex transformations into several stages, see Microsoft Knowledge Base article 320847, "HOW TO: Pipeline XSLT Transformations in .NET Applications," at http://support.microsoft.com/default.aspx?scid=kb;en-us;320847.


Minimize the Size of the Output Document

Try to keep the output document size to a minimum. If you are generating HTML, there are a couple of ways to do this.

First, use cascading style sheets to apply formatting instead of embedding formatting metadata in the HTML. Second, consider how your HTML is indented, and avoid unnecessary white space. To do so, set the indent setting to no as shown in the following XSLT fragment.


  <xsl:output method="html" indent="no"/>

By default, the value of the indent attribute is yes.


Write Efficient XSLT

When you develop XLST style sheets, start by making sure that your XPath queries are efficient. Here are some common guidelines for writing efficient XSLT style sheets:

  • Do not evaluate the same node set more than once. Save the node set in a <xsl:variable> declaration.
  • Avoid using the <xsl:number> tag if you can. For example, use the Position method instead.
  • Use the <xsl:key> tag to solve grouping problems.
  • Avoid complex patterns in template rules. Instead, use the <xsl:choose> tag in the rule.
  • Be careful when you use the preceding[-sibling] or the following[-sibling] axes. Use of these axes often involves algorithms that significantly affect performance.
  • Do not sort the same node set more than once. If necessary, save it as a result tree fragment, and then access it by using the node-set() extension function.
  • To output the text value of a simple #PCDATA element, use the <xsl:value-of> tag in preference to the <xsl:apply-templates> tag.
  • Avoid using inline script. Use extensions written in Microsoft Visual C# or Microsoft Visual Basic .NET to pass it as a parameter to the Transform call, and then bind to it by using the <xsl:param> tag. However, if you cache the style sheet in your application as described earlier, this achieves the same result. It then is perfectly acceptable to use script in the style sheet. In other words, this is just a compile-time issue.
  • Factor common queries into nested templates. For example, if you have two templates that match on "a/b/c" and "a/b/d," factor the templates into one common template that matches on "a/b." Have the common template call templates that match on "c" and "d."


References

For more information about XSLT processing, see the following Microsoft Knowledge Base articles:

Personal tools