다음을 통해 공유


XSLT 매개 변수

XSLT 매개 변수는 XsltArgumentList 메서드를 사용하여 AddParam에 추가됩니다. 정규화된 이름 및 네임스페이스 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로 변환됩니다.

다른 모든 유형은 오류를 발생시킵니다.

예시

다음 예제에서는 메서드를 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>  

참고하십시오