Debugging a Style Sheet

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

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, this section provides troubleshooting advice for the style sheet author.

Developing in the browser can be difficult, because the error can be in so many places and can be hidden by the browser. To see error messages, you can use a script at the command prompt, or run the MSXSL command prompt utility. For more information, see XSLT Utilities and Samples You Can Download.

The following are some common scenarios for debugging.

Problem:

No output is produced.

Solution:

When you view an XML file with an XSLT style sheet in a browser, errors in the source file or the style sheet are reported. These errors include XML that is not well-formed, as well as XSLT syntax errors and run-time errors. These errors are not automatically reported when you use XSLT from script or on data islands. For sample code to detect, format, and report these errors, see Performing Error Handling with XSLT. Alternatively, use direct browsing as a development tool for reporting errors. For more information, see Using the Default Style Sheet.

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 verifying that the root template of the style sheet is being executed. To do this, use the method outlined in the following section.

Problem:

The expected template is not being used.

Solution:

If the contents of a template, including the root template, are not appearing in the output, check to see if 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 anything.

If the trace message does not appear, the problem might 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 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.

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 are generated from the 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 display the content of the <address> element.

<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 might 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>? 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 the output.

Solution:

Elements from the XSLT namespace 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 therefore treats them as output elements.

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

Problem:

You need to highlight the elements which are 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

Displaying Transformation Results | Using the Default Style Sheet