逐步解說:偵錯 XSLT 樣式表Walkthrough: Debug an XSLT style sheet

此逐步教學中的步驟示範如何使用 XSLT 偵錯工具。The steps in this walkthrough demonstrate how to use the XSLT debugger. 這些步驟包括檢視變數、設定中斷點及逐步執行程式碼。Steps include viewing variables, setting breakpoints, and stepping through the code. 偵錯工具可讓您一次執行一行程式碼。The debugger lets you execute code one line at a time.

若要準備此逐步解說,請先將兩個 範例 檔案複製到本機電腦。To prepare for this walkthrough, first copy the two sample files to your local computer. 其中一個是樣式表單,而另一個是用來做為樣式表單輸入的 XML 檔。One is the style sheet, and one is the XML file we'll use as input to the style sheet. 在這個逐步解說中,我們使用的樣式表單會尋找其成本低於平均書籍價格的所有書籍。In this walkthrough, the style sheet we use finds all books whose cost is below the average book price.

注意

XSLT 偵錯工具僅適用于 Enterprise edition 的 Visual Studio。The XSLT debugger is only available in the Enterprise edition of Visual Studio.

開始偵錯Start debugging

  1. 選擇 [檔案 ] 功能表中的 [ 開啟 檔案] > **** 。From the File menu, choose Open > File.

  2. 找到 below-average .xsl 檔案,然後選擇 [ 開啟 ]。Locate the below-average.xsl file and choose Open.

    樣式表單會在 XML 編輯器中開啟。The style sheet opens in the XML editor.

  3. 按一下 [檔案屬性] 視窗中 [ 輸入 ] 欄位上的 [流覽] 按鈕 ( ... ) 。Click the browse button ( ... ) on the Input field of the document properties window. (如果看不到 [ 屬性 ] 視窗,請在編輯器中開啟之檔案的任何位置上按一下滑鼠右鍵,然後選擇 [ 屬性 ]。 ) (If the Properties window is not visible, right-click anywhere on the open file in the editor, and then choose Properties.)

  4. 找出 books.xml 檔案,然後選擇 [ 開啟 ]。Locate the books.xml file, and then choose Open.

    這會設定用於 XSLT 轉換的來源文件檔。This sets the source document file that's used for the XSLT transformation.

  5. below-average 的第12行上設定 中斷點Set a breakpoint on line 12 of below-average.xsl. 您可以透過下列其中一種方式來執行這項作業:You can do this in one of multiple ways:

    • 在第12行的編輯器邊界中按一下。Click in the margin of the editor on line 12.

    • 按一下第12行的任何位置,然後按 F9Click anywhere on line 12, and then press F9.

    • 以滑鼠右鍵按一下 [ xsl:if 開始] 標記,然後選擇 [ 中斷點 > 插入中斷點 ]。Right-click the xsl:if start tag, and then choose Breakpoint > Insert Breakpoint.

      在 Visual Studio 中的 XSL 檔案插入中斷點

  6. 在功能表列上,選擇 [ XML > 啟動 XSLT 調試 (],或按下 Alt + F5 ) 。On the menu bar, choose XML > Start XSLT Debugging (or, press Alt+F5 ).

    偵錯工具隨即啟動。The debugging process starts.

    在編輯器中,偵錯工具位於 xsl:if 樣式表單的元素上。In the editor, the debugger is positioned on the xsl:if element of the style sheet. 另一個名為 below-average.xml 的檔案會在編輯器中開啟;這是在處理輸入檔 books.xml 中的每個節點時,將填入的輸出檔。Another file named below-average.xml opens in the editor; this is the output file that will be populated as each node in the input file books.xml is processed.

    [自動變數]、[ 區域變數 ] 和 [****監看式 1] 視窗會出現在 Visual Studio 視窗的底部。The Autos , Locals , and Watch 1 windows appear at the bottom of the Visual Studio window. [ 區域變數 ] 視窗會顯示所有區域變數和其目前的值。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.

監看式視窗Watch window

我們會將兩個變數新增至 [ 監看式 1 ] 視窗,讓我們可以在處理輸入檔時檢查其值。We'll add two variables to the Watch 1 window so we can examine their values as the input file is processed. (如果您要監看的變數已經存在,您也可以使用 [ 區域變數 ] 視窗檢查值。 ) (You can also use the Locals window to examine values if the variables you want to watch are already there.)

  1. 在 [ 調試 ] 功能表中,選擇 [ Windows > watch > watch 1]From the Debug menu, choose Windows > Watch > Watch 1.

    [ 看式 1] 視窗隨即顯示。The Watch 1 window becomes visible.

  2. $bookAverage在 [ 名稱 ] 欄位中輸入,然後按 enterType $bookAverage in the Name field, and then press Enter.

    變數的值會 $bookAverage 顯示在 [ ] 欄位中。The value of the $bookAverage variable displays in the Value field.

  3. 在下一行中,輸入 self::node() [ 名稱 ] 欄位,然後按 enterOn the next line, type self::node() in the Name field, and then press Enter.

    self::node() 這是評估為目前內容節點的 XPath 運算式。self::node() is an XPath expression that evaluates to the current context node. self::node() XPath 運算式的值為第一個書籍節點。The value of the self::node() XPath expression is the first book node. 它會隨著轉換的進行而變更。This changes as we progress through the transformation.

  4. 展開 self::node() 節點,然後展開其值為的節點 priceExpand the self::node() node, and then expand the node who's value is price.

    在 Visual Studio 的 XSLT 調試期間監看式視窗

    您可以查看目前書籍節點的書籍價格值,並將其與值進行比較 $bookAverageYou can see the value of the book price for the current book node and compare it to the $bookAverage value. 由於書籍價格低於平均值,因此 xsl:if 當您繼續進行偵錯工具時,條件應該會成功。Because the book price is below the average, the xsl:if condition should succeed when you continue the debugging process.

逐步執行程式碼Step through the code

  1. F5 繼續。Press F5 to continue.

    因為第一個書籍節點滿足 xsl:if 條件,所以會將 book 節點新增至 below-average.xml 輸出檔。Because the first book node satisfied the xsl:if condition, the book node is added to the below-average.xml output file. 偵錯工具繼續執行,直到其再次定位於樣式表的 xsl:if 項目上為止。The debugger continues to execute until it is positioned again on the xsl:if element in the style sheet. 偵錯工具現在位於 books.xml 檔案的第二個書籍節點上。The debugger is now positioned on the second book node in the books.xml file.

    在 [ 監看式 1 ] 視窗中, self::node() 值會變更為第二個書籍節點。In the Watch 1 window, the self::node() value changes to the second book node. 藉由檢查價格項目的值,您可以確定價格高於平均值,因此 xsl:if 條件應該會失敗。By examining the value of the price element, you can determine that the price is above the average, thus the xsl:if condition should fail.

  2. F5 繼續。Press F5 to continue.

    因為第二個書籍節點不符合 xsl:if 條件,所以不會將 [書籍] 節點新增至 below-average.xml 輸出檔。Because the second book node does not meet the xsl:if condition, the book node is not added to the below-average.xml output file. 偵錯工具會繼續執行,直到它再次放置於 xsl:if 樣式表單中的元素上為止。The debugger continues to execute until it's positioned again on the xsl:if element in the style sheet. 偵錯工具現在位於books.xml檔案的第三個 book 節點 books.xml 上。The debugger is now positioned on the third book node in the books.xml file.

    在 [ 監看式 1 ] 視窗中, self::node() 值會變更為第三個書籍節點。In the Watch 1 window, the self::node() value changes to the third book node. 藉由檢查項目的值 price ,您可以判斷價格低於平均值。By examining the value of the price element, you can determine that the price is below the average. xsl:if條件應該會成功。The xsl:if condition should succeed.

  3. F5 繼續。Press F5 to continue.

    因為 xsl:if 已滿足條件,所以會將第三本書新增至 below-average.xml 輸出檔。Because the xsl:if condition was satisfied, the third book is added to the below-average.xml output file. 當處理過 XML 文件中的所有書籍後,偵錯工具會停止。All books in the XML document have been processed and the debugger stops.

範例檔案Sample files

逐步教學會使用下列兩個檔案。The following two files are used by the walkthrough.

below-average .xslbelow-average.xsl

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://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 &lt; $bookAverage">
          <xsl:copy-of select="."/>
        </xsl:if>
      </xsl:for-each>
    </books>
  </xsl:template>
</xsl:stylesheet>

books.xmlbooks.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>

請參閱See also