XSLT <xsl:when> 요소

정의와 사용법

<xsl:when> 요소는 <xsl:choose> 요소에 대한 관련 작업을 지정합니다.

<xsl:when> 요소는 표현식을 계산하며, 반환 값이 true면 지정된 작업을 수행합니다.

주의사항:<xsl:when> 요소는 <xsl:choose> 요소와 <xsl:otherwise> 요소와 관련된 여러 조건 테스트를 제공합니다.

문법

<xsl:when test="boolean-expression">
  <!-- 내용: 템플릿 -->
</xsl:when>

속성

속성 설명
테스트 boolean-expression 필수. 테스트할 부울 표현식을 지정합니다.

예제

예제 1

下面的代码会在 cd의 price가 10보다 높을 때 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 배경색="#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 배경색="#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>

XML 파일 확인,XSL 파일 확인,결과 확인.

예제 2

이름이 "color"인 변수를 선언했습니다. 그 값을 현재 요소의 color 속성에 할당합니다. 현재 요소에 color 속성이 없으면 "color"의 값은 "green"입니다:

<xsl:variable name="color">
  <xsl:choose>
    <xsl:when test="@color">
      <xsl:value-of select="@color"/>
    </xsl:when>
    <xsl:otherwise>green</xsl:otherwise>
  </xsl:choose>
</xsl:variable>