XSLT <xsl:choose> အစိတ်

အသုံးပြုခြင်း နှင့် သဘော

<xsl:choose> အရာ နှင့် <xsl:when> နှင့် <xsl:otherwise> အရာ ပေါင်းစပ် လုပ်ကိုင် သော် အချိန်ကြား စစ်ဆေးခြင်း နှစ်ခု ကို ကိုယ်စားပြုနိုင်သည်。

ဤ <xsl:when> အခြေအနေ မှာ true မဟုတ် ဖြစ်ခဲ့ လျှင် <xsl:otherwise> အရာ ကို ဖြေရှင်းပါ。

အရာ၏ <xsl:when> သည် true ဖြစ်ခြင်း မရှိဘဲကြားခင်းကား အရာ၏ <xsl:otherwise> အရာ ရှိခြင်း မရှိဘဲကြားခင်းကား အကြောင်းအရာ တစ်ခုခု ကို ဖန်တီးလိမ့်မည် မဟုတ်ပါ။

အကြံပေးလက်ရှိအခြေအနေမှာ လုပ်ငန်းကို စက်တင်းပြီး အခြေအနေကို သုံးပါ。

အက္ခရာ

<xsl:choose>
<!-- Content:(xsl:when+,xsl:otherwise?) -->
</xsl:choose>

အခြေအနေ

None

အမှုထမ်း

လောက် 1

CD အဆုံးဖြတ် အရေအတွက် 10 ထက် မြင့်ကြားသောအခါ အဆိုပါ artist စာရင်းသို့ ပေါင်းစပ်သော အမှုန်းအရောင် ပြုလုပ်ပါ:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

XML ဖိုင် ကို လေ့လာပါ၊ XSL ဖိုင် ကို လေ့လာပါ၊ အကျိုးသတ္တု ကို လေ့လာပါ。

လောက် 2

အမည် "color" ဟု သတင်းမျိုး တစ်ခု ကို ပြင်ဆင်ပါ။ ထိုသတင်းမျိုး၏ ပြင်ဆင်ချက် ကို current အဆိုပါ အရာ၏ color အကိုးအကား သို့ သတ်မှတ်ပါ။ အရာ၏ color အကိုးအကား ရှိခြင်း မရှိဘဲကြားခင်းကား color အကိုးအကား သည် "green" ဖြစ်ပါသည်:

<xsl:variable name="color">
  <xsl:choose>
    <xsl:when test="@color">
      <xsl:value-of select="@color"/>
    </xsl:when>  
    <xsl:otherwise>green</xsl:otherwise>
  </xsl:choose>
</xsl:variable>