XSLT <xsl:for-each> 요소

정의와 사용법

<xsl:for-each> 요소는 지정된 노드 집합의 각 노드를 순회할 수 있습니다.

문법

<xsl:for-each
select="표현식">
  <!-- Content:(xsl:sort*,template) -->
</xsl:for-each>

속성

속성 설명
select 표현식 필수. 처리할 노드 집합.

예제

예제 1

순회하며 각 "cd" 요소를 돌아다니고, 각 title과 artist를 출력에 <xsl:value-of>로 작성합니다:

<?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>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

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

예제 2

순회하며 각 "cd" 요소를 돌아다니고, 각 title과 artist를 출력에 <xsl:value-of>로 작성합니다 (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 bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

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