將 <xsl:choose> 元素新增至 XSL 檔中

<xsl: choose> 元素使您可以從一系列的替代項目中選取一個選項。您可以利用它從許多選項中選取,如有需要,它也接受預設選項。

其組成如下:
<xsl: choose> 
	<xsl: when test ="Boolean expression"> 
	<xsl: when test ="Boolean expression"> 
	<xsl:otherwise> 
</xsl: choose>

每一個 <xsl: when> 元素都有一個測試條件。該測試條件是一個 Boolean 表示式,即轉換成 Boolean 值的 XPath 表示式。系統會將測試條件為 true 的第一個 <xsl: when> 元素的內容輸出到結果樹。

同樣地,只有當所有 <xsl: when> 元素的測試條件都不是 true 時,<xsl: choose> 元素才可以有選用的 <xsl:otherwise> 元素(其內容只供輸出)。如果沒有 <xsl:otherwise> 元素存在,而且任何 <xsl: when> 子元素的測試條件都不是 true,<xsl: choose> 元素就不會產生任何輸出。

以下是 <xsl: choose> 元素的範例:

<xsl: choose> 
  <xsl: when test = "number [ . &gt; 2000]">A big number</xsl:when> 
 <xsl:otherwise>A small number</xsl:otherwise> 
</xsl:choose>

如果目標 XML 檔的數字元素包含大於 2,000 的數值,系統就會將字串 "A big number" 插入結果樹中。其他狀況下,則會插入 "A small number"。

以下是針對「資源」視景撰寫的指示, 但是它們也適用於許多其他視景。

如果要建立 <xsl: choose> 元素,請遵循下列步驟:

  1. 開啟您要新增 <xsl: choose> 元素的 XSL 檔。
  2. 在 XSL 編輯器中,將游標插入要新增 <xsl: choose> 元素的位置。
  3. 按一下片段標籤,開啟 XSL 抽屜, 然後按兩下 xsl:choose 元素的條件式邏輯 這時便會開啟「XSL 條件式處理」精靈。
  4. 按一下新增 When 來新增 <xsl: when> 條件。選取它。
  5. 測試欄位中,輸入 XPath 表示式。 您可以手動輸入它,或利用 XPath 表示式精靈來建立它(按一下 XPath 來啟動它)。
  6. 內容欄位中,輸入當測試條件成真時,要新增至結果樹的內容。
  7. 針對於您想要新增的任何其他 <xsl: when> 條件,重複第 4-6 步驟。
  8. (選用)按一下新增 Otherwise 來新增 <xsl:otherwise> 條件。 選取它。 您不能指定測試條件。
  9. 內容欄位中,輸入如果沒有 <xsl: when> 元素為 true 時,要新增至結果樹的內容。
  10. 變成灰色的區域會顯示 <xsl: choose> 陳述式的內容。 您無法直接在這裡編輯它。
  11. 如果要編輯條件,請選取它,並在必要時編輯欄位。
  12. 如果要移除條件,請選取它並按一下移除
  13. 當完成了 <xsl:choose> 元素時,請按一下完成
<xsl:choose> 元素會插入 XSL 檔中。
使用條款 | 讀者意見
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.