XSLT <xsl:choose> အရာ

XSLT <xsl:choose> အသုံးပြုသည် <xsl:when> နှင့် <xsl:otherwise> ကို နှိုင်းယှဉ်မှုများ ကို ကိုယ်စားပြုသည်။

<xsl:choose> အရာ

ဘာသာစကား

<xsl:choose>
  <xsl:when test="expression">
    ... ထုတ်လုပ် ...
  </xsl:when>
  <xsl:otherwise>
    ... ထုတ်လုပ်ခြင်း ...
  </xsl:otherwise>
</xsl:choose>

အချက်အလက် ကြည့်ရှုရာ

XML ဖိုင်အတွက် အချက်အလက် အပိုင်းအစား ပြုလုပ်ခြင်း အတွက် သင့် <xsl:choose>、<xsl:when> နှင့် <xsl:otherwise> အသုံးပြုပါ:

<?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>

အခြား CD အဆင်းပြောင်းသည် 10 ပြီးနောက် အသုံးပြုသော "Artist" လိုင်းသို့ အသင်းပြောင်းကြောင်း အစိတ်အပိုင်းပြုထားသည်:

上面的转换结果类似这样:

ဒီ XML ဖိုင်ကို ကြည့်ပါဒီ XSL ဖိုင်ကို ကြည့်ပါ查看结果

အခြား အမှတ်အသား

ဒီမှာ နှစ်ခုစလုံး <xsl:when> အရာတစ်ခု ရှိသော အခြား အမှတ်အသားတစ်ခုဖြစ်သည်:

<?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:when test="price > 9">
            <td bgcolor="#cccccc">
            <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>

上面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色,并在 CD 的价格高于 9 且低于等于 10 时向 "Artist" 列添加灰色的背景颜色。

上面的转换结果类似这样:

查看此XML文件查看此XSL文件查看结果