XSLT 매개 변수

XsltArgumentList 메서드를 사용하여 AddParam에 XSLT 매개 변수를 추가합니다. 이 때 정규화된 이름 및 네임스페이스 URI가 매개 변수 개체와 연결됩니다.

XSLT 매개 변수를 사용하려면

  1. XsltArgumentList 메서드를 사용하여 AddParam 개체를 만들고 매개 변수를 추가합니다.

  2. 스타일시트에서 매개 변수를 호출합니다.

  3. XsltArgumentList 개체를 Transform 메서드에 전달합니다.

매개 변수 형식

매개 변수 개체는 W3C 형식과 일치해야 합니다. 다음 표에서는 해당 W3C 형식, 해당 Microsoft .NET 클래스(형식) 및 W3C 형식이 XPath 형식인지 또는 XSLT 형식인지를 보여 줍니다.

W3C 형식 해당 .NET 클래스(형식) XPath 또는 XSLT 형식
String System.String XPath
Boolean System.Boolean XPath
Number System.Double XPath
Result Tree Fragment System.Xml.XPath.XPathNavigator XSLT
Node* System.Xml.XPath.XPathNavigator XPath
Node Set XPathNodeIterator

XPathNavigator[]
XPath

*단일 노드가 포함된 노드 세트와 같습니다.

매개 변수 개체가 위 클래스에 해당하지 않으면 다음 규칙에 따라 변환됩니다. CLR(공용 언어 런타임) 숫자 형식은 Double로 변환됩니다. DateTime 형식은 String으로 변환되고, IXPathNavigable 형식은 XPathNavigator로 변환됩니다. XPathNavigator[]XPathNodeIterator로 변환됩니다.

다른 모든 형식은 오류를 throw합니다.

예시

다음 예제에서는 AddParam 메서드를 사용하여 계산된 할인 기간을 유지하는 매개 변수를 만듭니다. 할인 기간은 주문 날짜로부터 20일 동안으로 계산됩니다.

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public class Sample {

   public static void Main() {

      // Create the XslCompiledTransform and load the style sheet.
      XslCompiledTransform xslt = new XslCompiledTransform();
      xslt.Load("discount.xsl");

      // Create the XsltArgumentList.
      XsltArgumentList argList = new XsltArgumentList();

      // Calculate the discount date.
      DateTime orderDate = new DateTime(2004, 01, 15);
      DateTime discountDate = orderDate.AddDays(20);
      argList.AddParam("discount", "", discountDate.ToString());

      // Create an XmlWriter to write the output.
     XmlWriter writer = XmlWriter.Create("orderOut.xml");

     // Transform the file.
     xslt.Transform(new XPathDocument("order.xml"), argList, writer);
     writer.Close();
  }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl

public class Sample

    public shared sub Main()

        ' Create the XslCompiledTransform and load the style sheet.
        Dim xslt as XslCompiledTransform = new XslCompiledTransform()
        xslt.Load("discount.xsl")

        ' Create the XsltArgumentList.
        Dim argList as XsltArgumentList = new XsltArgumentList()

        ' Calculate the discount date.
        Dim orderDate as DateTime = new DateTime(2004, 01, 15)
        Dim discountDate as DateTime = orderDate.AddDays(20)
        argList.AddParam("discount", "", discountDate.ToString())

        ' Create an XmlWriter to write the output.             
        Dim writer as XmlWriter = XmlWriter.Create("orderOut.xml")

        ' Transform the file.
        xslt.Transform(new XPathDocument("order.xml"), argList, writer)
        writer.Close()

    end sub

end class

입력

order.xml
<!--Represents a customer order-->
<order>
  <book ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <cd ISBN='2-3631-4'>
    <title>Americana</title>
    <price>16.95</price>
  </cd>
</order>
discount.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="discount"/>
  <xsl:template match="/">
    <order>
      <xsl:variable name="sub-total" select="sum(//price)"/>
      <total><xsl:value-of select="$sub-total"/></total>
           15% discount if paid by: <xsl:value-of select="$discount"/>
     </order>
  </xsl:template>
</xsl:stylesheet>

출력

<?xml version="1.0" encoding="utf-8"?>  
<order>  
  <total>36.9</total>  
     15% discount if paid by: 2/4/2004 12:00:00 AM  
</order>  

참고 항목