노드 목록에서 선택된 모든 노드에 상대적인 노드의 위치나 인덱스 번호를 반환합니다.
number position()
주의
노드 위치는 1부터 시작하므로 첫 번째 노드는 위치 1을 반환합니다.
예제
다음 코드 예제에서는 position()
함수를 사용한 결과를 보여 줍니다.
XML 파일(position.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="position.xsl"?>
<test>
<x a="a11">
<x a="a21">
<x a="a31">
<y b="b31">y31</y>
<y b="b32">y32</y>
</x>
</x>
</x>
<x a="a12">
<x a="a22">
<y b="b21">y21</y>
<y b="b22">y22</y>
</x>
</x>
<x a="a13">
<y b="b11">y11</y>
<y b="b12">y12</y>
</x>
<x a="a14">
<y b="b01">y01</y>
<y b="b02">y02</y>
</x>
</test>
XSLT 파일(position.xsl)
<?xml version='1.0'?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//x"/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*"/>
<xsl:value-of select="position()"/>
</xsl:element>\n
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
출력
위의 XSLT 스타일시트는 소스 XML 파일에 적용할 경우 문서 순서상 위치가 고정되어 있는 내용을 가진 새 <x>
요소에 대해 <x>
요소를 모두 매핑합니다.
<x a="a11">1</x>
<x a="a21">2</x>
<x a="a31">3</x>
<x a="a12">4</x>
<x a="a22">5</x>
<x a="a13">6</x>
<x a="a14">7</x>
작동하는 컨텍스트에 대해 position()
함수의 민감도를 확인하기 위해 위의 XSLT 파일에서 다음과 같이 템플릿 규칙을 바꿔 봅시다.
<xsl:template match="/">
<xsl:apply-templates select="//x"/>
</xsl:template>
이 템플릿 규칙을 사용할 경우
<xsl:template match="/">
<xsl:apply-templates select="//x[1]"/>
</xsl:template>
결과는 다음과 같습니다.
<x a="a11">1</x>
<x a="a21">2</x>
<x a="a31">3</x>
<x a="a22">4</x>
그러나 템플릿 규칙을 다음으로 대체할 경우
<xsl:template match="/">
<xsl:apply-templates select="//x[2]"/>
</xsl:template>
결과는 다음과 같습니다.
<x a="a12">1</x>