XSLT <xsl:if> 요소

정의 및 사용법

<xsl:if>은 템플릿을 포함하고 있으며, 지정된 조건이 성립할 때만 해당 템플릿이 적용됩니다。

휴대폰: <xsl:choose>와 <xsl:when> 및 <xsl:otherwise>를 함께 사용하여 복잡한 조건 테스트를 표현하세요!

문법

<xsl:if
test="표현식">
<!-- Content: template -->
</xsl:if>

속성

속성 설명
테스트 표현식 필수. 테스트할 조건을 정의합니다。

예제

예제 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 > 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 제목 사이에 ", "를 삽입합니다. 마지막 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>