XSLT <xsl:choose> 요소

XSLT <xsl:choose> 요소는 <xsl:when>과 <xsl:otherwise>를 결합하여 중복 조건 테스트를 표현하는 데 사용됩니다.

<xsl:choose> 요소

문법

<xsl:choose>
  <xsl:when test="표현식">
    ... 출력 ...
  </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>저의 CD 컬렉션</h2>
    <table border="1">
      <tr 배경색="#9acd32">
        <th>제목</th>
        <th>아티스트</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td 배경색="#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보다 높을 때 "아티스트" 열에 핑크색 배경색을 추가합니다.

위의 변환 결과는 이렇게 보입니다:

이 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>저의 CD 컬렉션</h2>
    <table border="1">
      <tr 배경색="#9acd32">
        <th>제목</th>
        <th>아티스트</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td 배경색="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:when test="price > 9">
            <td 배경색="#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 파일을 확인결과 확인