Debugging a Style Sheet

Because the interaction of a style sheet with a data file can be complex, and style sheet errors are not always obvious, the following information provides troubleshooting advice for the style sheet author.

Problem

No output at all.

Solution

When browsing to an XML file with an XSLT style sheet, well-formed errors in the source file or the style sheet are reported, along with XSLT syntax errors and run-time errors. These errors are not automatically reported when using XSLT from script.

If no errors are reported, the problem is most likely an error in the style sheet design, which can be diagnosed and corrected by the style sheet author. Begin by testing that the root template of the style sheet is being executed, using the method outlined in the following section.

Problem

Expected template is not being used.

Solution:

If the contents of a template, including the root template, are not appearing in the output, verify the template is being called by adding an explicit trace message.

<xsl:template match="address">
  !! inside address template !!
  <xsl:apply-templates select="zipcode"/>
</xsl:template>

If the trace message appears in the output, the template is being executed, and the problem is likely that this <xsl:apply-templates> is not returning any output. For more information about why the select pattern returns no results, see No Results from Select Pattern below.

If the trace message does not appear, the problem could be that the match pattern is in error. Check the spelling and case of element and attribute names, and check that the full context for the match is actually present in the source data.

Also determine which <xsl:apply-templates> element should invoke the template and ensure that it is selecting the correct nodes. For more information, see the No Results from Select Pattern problem.

If the template uses the root pattern (/) and is not being called, check that no template later in the style sheet uses the root pattern or omits the match attribute (matching all nodes, including the document root). If the style sheet is being executed against a node other than the document root, a template matching that element is used instead of the root template.

Problem

No results from select pattern.

Solution

One of the most frequent errors preventing output is select patterns that do not match the input data. Fortunately, these are easy to fix. The debug line added to the example below uses <xsl:value-of> to output the node and its children as preformatted XML source.

<xsl:template match="address">
  <PRE><xsl:value-of select="."/></PRE>
  <xsl:apply-templates select="zipcode"/>
</xsl:template>

When comparing the actual XML data to the select pattern, you may find spelling mistakes in element and attribute names; for example, the data could show "zip-code" or "zipCode" as the correct element name. The context of the query can be checked. Is "zipcode" a child of "address"? And the source data can also be checked for accuracy. Does this "address" have a "zipcode"?

For complex patterns, it is often helpful to progressively simplify the pattern to isolate the problem.

Problem

Elements with the XSLT namespace appear in output.

Solution

Elements from the XSLT name space appearing in the output indicate that the XSLT namespace declaration is using an incorrect URL. The XSLT processor does not recognize these elements as XSLT elements and treats them as output elements.

The correct namespace identifier for XSLT is "http://www.w3.org/1999/XSL/Transform".

Problem

Highlighting elements not handled by a template.

Solution

Often it is helpful to author a style sheet incrementally, adding templates for elements one at a time. Adding this template to your style sheet will show you visually those elements that do not yet have templates associated with them.

<xsl:template match="*">
  <SPAN STYLE="background-color:yellow">
    <xsl:attribute name="title">&lt;<xsl:value-of select="."/>&gt;</xsl:attribute>
    <xsl:apply-templates/>
  </SPAN>
</xsl:template>

Add this template near the top of your style sheet so that it does not override templates that handle specific elements. This template will display a yellow background behind the content of any element without a specific template in existence. Running the mouse over the yellow areas will display a ToolTip showing those elements that must still be handled by templates.

See Also

Advanced XSLT Features

 Last updated on Saturday, April 10, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.