카테고리 없음

제한적 반복문

아거스 2010. 12. 9. 01:40

XSLT에서는 기본적으로 for-each라는 구문이 있습니다.
이는 일반 언어에서 말하는 while문에 해당됩니다. 즉, 해당 노드의 수 만큼 무조건적 반복을 합니다.
게다가 break 명령 또한 없기에 프로그래밍을 할 때 어려운 점이 많습니다.
이런경우 call-template문을 사용한 recursive call을 할 수 있는 template을 작성해서 사용하면 편리하게 프로그래밍을 할 수 있습니다.

for ( var i = 1; i <= 10; ++i) {
   // 반복되며 수행 할 명령
}
위와 같은 for문이 있다고 가정하면 XSLT에서는 아래와 같은방식으로 처리 할 수 있습니다. 

<xsl:call-template name="for">
   <xsl:with-param name="from" select="1" />
   <xsl:with-param name="to" select="10"/>
</xsl:call-template>

<xsl:template name="for">
   <xsl:param name="from"></xsl:param>
   <xsl:param name="to"></xsl:param>
   <xsl:if test="$from <= $to">
      <!-- 반복되며 수행 할 명령 -->
      <xsl:call-template name="for">
         <xsl:with-param name="from" select="$from + 1"></xsl:with-param>
         <xsl:with-param name="to" select="$to"></xsl:with-param>
      </xsl:call-template>
   </xsl:if>
</xsl:template>