XSLT <xsl:choose> 요소
정의와 사용법
xsl:choose 요소와 xsl:when 및 xsl:otherwise 요소를 결합하여 복잡한 조건 검사를 표현할 수 있습니다。
xsl:when이 true가 아니면, xsl:otherwise의 내용을 처리합니다。
xsl:when이 true가 아니고, xsl:otherwise 요소가 없으면, 어떤 내용도 생성되지 않습니다。
추천:간단한 조건 검사에 대해서는 <xsl:if> 요소를 대신 사용하세요。
문법
<xsl:choose> <!-- Content:(xsl:when+,xsl:otherwise?) --> </xsl:choose>
속성
None
예시
예제 1
아래 코드는 CD의 가격이 10보다 높을 때, artist 열에 분홍색 배경색을 추가합니다:
<?xml 버전="1.0" 인코딩="ISO-8859-1"?> <xsl:stylesheet 버전="1.0"> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>저의 CD 컬렉션</h2> <table 기본선="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>
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>