XSLT <xsl:선택>
❮ XSLT 요소 참조
정의 및 사용
<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>
속성
없음
예
아래 코드는 CD 가격이 10보다 높을 때 아티스트 열에 분홍색 배경색을 추가합니다.
예시
<?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>
"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>
❮ XSLT 요소 참조