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 ఫైల్నిలో <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>

సిడీ ధర మీద రెండవ దశలో ఉన్నప్పుడు "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文件查看结果