다음을 통해 공유


<xsl:apply-imports>의 예제 1

이 예제에서는 <xsl:apply-imports> 를 사용하여 코드를 더욱 효과적으로 다시 사용하는 방법을 보여 줍니다.이 예제에서는 네 가지 기본 파일을 사용합니다.

  • XML 소스 파일 ops.xml. 이 데이터 파일에서는 add(+), sub(-) 및 mul(*)의 세 가지 연산을 정의합니다.

  • 기본 XSLT 스타일시트 ops.xsl. 이 파일에는 두 <xsl:import> 요소를 포함하여 작업에 대한 템플릿 규칙이 들어 있습니다. 가져온 스타일시트는 지정된 데이터 소스에서 산술 연산 및 문자열 연산을 수행합니다.

  • 가져온 스타일시트 arith.xsl. 이 XSLT 파일은 각 <op> 요소에서 산술 연산을 수행합니다.

  • 가져온 또 다른 스타일시트 str.xsl. 이 XSLT 파일은 사용자 지정 문자열 연산을 수행합니다. 여기서 add(+)는 문자열을 연결합니다. 예를 들어, 1+212가 됩니다. 마찬가지로 mul(*)은 문자열을 반대로 연결합니다. 예를 들어, 1*2는 21이 됩니다. sub(-)는 정의되지 않은 문자열 연산입니다.

XML 파일(ops.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ops.xsl"?>
<ops>
  <desc>Some binary operations</desc>
  <op name="add" symbol="+">
    <operand>1</operand>
    <operand>2</operand>
  </op>
  <op name="sub" symbol="-">
    <operand>1</operand>
    <operand>2</operand>
  </op>
  <op name="mul" symbol="*">
    <operand>1</operand>
    <operand>2</operand>
  </op>
</ops>

기본 XSLT 파일(ops.xsl)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0">
  <xsl:import href="arith.xsl"/>
  <xsl:import href="str.xsl"/>
  <xsl:template match="op">
    <xsl:value-of select="operand[1]"/>
    <xsl:value-of select="@symbol"/>
    <xsl:value-of select="operand[2]"/>
    = <xsl:apply-imports/>
    <br/>
  </xsl:template>
</xsl:stylesheet>

가져온 XSLT 파일(arith.xsl)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0">
  <xsl:template match="op[@symbol='+']">
    <xsl:value-of select="sum(operand)"/> (from arith.xsl)
  </xsl:template>
  <xsl:template match="op[@symbol='-']">
    <xsl:value-of select="number(operand[1])-number(operand[2])"/> 
   (from arith.xsl)
  </xsl:template>
  <xsl:template match="op[@symbol='*']">
    <xsl:value-of select="number(operand[1])*number(operand[2])"/> 
    (from arith.xsl)
  </xsl:template>
</xsl:stylesheet>

가져온 XSLT 파일(str.xsl)

<?xml version="1.0"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                  version="1.0">
  <xsl:template match="desc">
    <DIV><xsl:value-of select="."/></DIV>
  </xsl:template>
  <xsl:template match="op[@name='add']">
    <xsl:value-of select="operand[1]"/>
    <xsl:value-of select="operand[2]"/> (from str.xsl)
  </xsl:template>
  <xsl:template match="op[@name='mul']">
    <xsl:value-of select="operand[2]"/>
    <xsl:value-of select="operand[1]"/> (from str.xsl)
  </xsl:template>
</xsl:stylesheet>

출력

출력은 다음과 같습니다.

Some binary operations

1+2 = 12 (from str.xsl)

1-2 = -1 (from arith.xsl)

1*2 = 21 (from str.xsl)

주의

마지막으로 가져온 스타일시트는 가져오기 우선 순위가 가장 높습니다. 이 예제에서는 str.xsl을 마지막으로 가져왔으므로 arith.xsl보다 가져오기 우선 순위가 높습니다. 가져온 스타일시트 두 개에 모두 addmul 연산에 대한 템플릿이 있지만 str.xsl의 템플릿만 호출됩니다. 그러나 str.xsl에 sub 연산이 정의되지 않았으므로 arith.xsl에 정의된 sub 연산이 사용됩니다. 기본 XSLT 파일에서 <xsl:import> 요소의 순서를 다음과 같이 바꾸었다고 가정합니다.

<xsl:import href="str.xsl"/>

<xsl:import href="arith.xsl"/>

이 경우 출력은 다음과 같습니다.

Some binary operations

1+2 = 3 (from arith.xsl)

1-2 = -1 (from arith.xsl)

1*2 = 2 (from arith.xsl)

또한 기본 XSLT 파일(ops.xsl)에서 <op>에 대한 재정의 템플릿 규칙에 <xsl:apply-imports/> 명령이 없을 경우 출력은 다음과 같습니다.

Some binary operations

1+2 =

1-2 =

1*2 =

즉, 스타일시트를 가져오는 템플릿 규칙은 가져온 스타일시트에서 관련 템플릿 규칙을 재정의합니다. <xsl:apply-imports/> 명령을 사용하면 재정의된 템플릿 규칙을 다른 방법으로 다시 활성화할 수 있습니다.