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, search the Microsoft Web site for "msxsl.exe".
The following are some common scenarios for debugging.
No output is produced.
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.
The expected template is not being used.
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.
No results are generated from the
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
<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
<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
For complex patterns, it is often helpful to progressively simplify the pattern to isolate the problem.
Elements with the XSLT namespace appear in the output.
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".
You need to highlight the elements which are not handled by a template.
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"><<xsl:value-of select="."/>></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.