XSLT <xsl:choose> 요소
<xsl:choose> 요소는 <xsl:when> 및 <xsl:otherwise>와 함께 사용되어 여러 조건부 테스트를 표현합니다.
<xsl:choose> 요소
통사론
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
선택 조건을 넣을 위치
XML 파일에 대해 다중 조건부 테스트를 삽입하려면 <xsl:choose>, <xsl:when> 및 <xsl:otherwise> 요소를 XSL 파일에 추가합니다.
예시
<?xml version="1.0" encoding="UTF-8"?>
<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보다 높을 때 "아티스트" 열에 분홍색 배경색을 추가합니다.
또 다른 예
다음은 두 개의 <xsl:when> 요소를 포함하는 또 다른 예입니다.
예시
<?xml version="1.0" encoding="UTF-8"?>
<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보다 높으면 "아티스트" 열에 분홍색 배경색을 추가하고 CD 가격이 9보다 높고 10보다 낮거나 같으면 회색 배경색을 추가합니다.