XSLT <xsl:if> ÔªËØ

¶¨ÒåºÍÓ÷¨

<xsl:if> °üº¬ÁËÒ»¸öÄ£°å£¬Ö»ÓÐÖ¸¶¨µÄÌõ¼þ³ÉÁ¢Ê±£¬²ÅÓ¦ÓôËÄ£°å¡£

Ìáʾ£ºÇëʹÓà <xsl:choose> Óë <xsl:when> ºÍ <xsl:otherwise> ½áºÏ£¬À´±í´ï¶àÖØÌõ¼þ²âÊÔ£¡

Óï·¨

<xsl:if
test="expression">

<!-- Content: template -->

</xsl:if>

ÊôÐÔ

ÊôÐÔ Öµ ÃèÊö
test expression ±ØÐè¡£¹æ¶¨Òª²âÊÔµÄÌõ¼þ¡£

ʵÀý

Àý×Ó 1

µ± CD µÄ¼Û¸ñ¸ßÓÚ 10 ʱ£¬Ñ¡È¡ title ºÍ 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">
      <xsl:if test="price &gt; 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

²é¿´ XML Îļþ£¬²é¿´ XSL Îļþ£¬²é¿´½á¹û¡£

Àý×Ó 2

ÏÔʾÿ¸ö CD µÄ±êÌâ¡£Èç¹û²»ÊÇ×îºóÒ»¸ö»òµ¹ÊýµÚ¶þ¸ö CD£¬ÔòÔÚÿ¸ö CD-title ¼ä²åÈë ", "¡£Èç¹ûÊÇ×îºóÒ»¸ö CD£¬ÔòÔÚ±êÌâºóÌí¼Ó "!"¡£Èç¹ûÊǵ¹ÊýµÚ¶þ¸ö CD£¬ÔòÔÚÆäºóÌí¼Ó ", and "£º

<?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>
    <p>Titles:
    <xsl:for-each select="catalog/cd">
      <xsl:value-of select="title"/>
      <xsl:if test="position()!=last()">
        <xsl:text>, </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()-1">
        <xsl:text> and </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()">
        <xsl:text>!</xsl:text>
      </xsl:if>
    </xsl:for-each>
    </p>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
VUE