XSLT <xsl:choose> 요소
- 이전 페이지 XSLT <if>
- 다음 페이지 XSLT Apply
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>를 XSL 파일에 추가하세요:
<?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"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#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보다 높을 때 "Artist" 열에 핑크색 배경색을 추가합니다.
위의 변환 결과는 이렇게 보입니다:

다른 예제
이 또 다른 예제는 두 개의 <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>My CD Collection</h2> <table border="1"> <tr bgcolor="#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 bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price > 9"> <td bgcolor="#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" 열에 회색 배경색을 추가합니다.
위의 변환 결과는 이렇게 보입니다:

- 이전 페이지 XSLT <if>
- 다음 페이지 XSLT Apply