Walkthrough: Debug an XSLT Style Sheet
The steps in this walkthrough demonstrate how to use the XSLT debugger. Steps include viewing variables, setting breakpoints, and stepping through the code.
To prepare for this walkthrough
Close any open solutions.
Copy the two sample files to your local computer.
Start Debugging
To start debugging
From the File menu, point to Open, and click File.
Locate the belowAvg.xsl file and click Open.
The style sheet is opened in the XML Editor.
Click the browse button (...) on the Input field of the document properties window.
Locate the books.xml file and click Open.
This sets the source document file that is used for the XSLT transformation.
Right-click the
xsl:for-each
start tag, point to Breakpoint, and click Insert Breakpoint.Right-click the
xsl:if
start tag, point to Breakpoint, and click Insert Breakpoint.Click the Debug XSL button on the XML Editor toolbar.
This starts the debugging process and opens several new windows that are used by the debugger.
The Locals window displays all the local variables and their current values. This includes variables defined in the style sheet and also variables that the debugger uses to track the nodes that are currently in context.
The XSL Output window displays the output of the XSL transformation. This window is separate from the Visual Studio Output window.
Watch Window
To use the Watch window
From the Debug menu, point to Windows, point to Watch, and click Watch 1.
This makes the Watch 1 window visible.
Type
$bookAverage
in the Name field and press ENTER.The value of the
$bookAverage
variable is displayed in the window.Type
self::node()
in the Name field and press ENTER.self::node()
is a pre-defined context variable that tracks the current context. The value of theself::node()
variable is the root node. This changes as we progress through the transformation.
Step Through the Code
The debugger enables you to execute code one line at a time.
To step through the code
Press F10 to step into the code.
The debugger advances to the
xsl:if
element. Theself::node()
value changes to the first book node.The debugger opens the XML source document in a new window and shows the current execution node.
In the Watch1 window, expand the
self::node()
node, and expand the price node.This allows you to see the value of the book price and you can easily compare it to the
$bookAverage
value. Because the book price is below the average, thexsl:if
condition should succeed.Press F5 to continue.
Because the first book node satisfied the
xsl:if
condition, the book node is added to the XSL output window. The debugger continues to execute until it is positioned on thexsl:if
condition for the second book. By examining the value of the price element, you can determine that the price is above the average, thus thexsl:if
condition should fail.Press F5 to continue.
The debugger is now on the
xsl:if
condition for the third book. Because the book price is above the average, thexsl:if
condition should pass.Press F5 again to continue.
Because the
xsl:if
condition was satisfied, the third book is added to the XSL Output window. All books in the XML document have been processed and the debugger stops.
Sample Files
The following two files are used by the walkthrough.
belowAvg.xsl
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="/">
<xsl:variable name="bookCount" select="count(/bookstore/book)"/>
<xsl:variable name="bookTotal" select="sum(/bookstore/book/price)"/>
<xsl:variable name="bookAverage" select="$bookTotal div $bookCount"/>
<books>
<!--Books That Cost Below Average-->
<xsl:for-each select="/bookstore/book">
<xsl:if test="price < $bookAverage">
<book>
<xsl:copy-of select="node()"/>
</book>
</xsl:if>
</xsl:for-each>
</books>
</xsl:template>
</xsl:stylesheet>
books.xml
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>