XsltArgumentList.AddExtensionObject(String, Object) 메서드

정의

XsltArgumentList에 새 개체를 추가하고 이를 네임스페이스 URI와 연결합니다.

public:
 void AddExtensionObject(System::String ^ namespaceUri, System::Object ^ extension);
public void AddExtensionObject (string namespaceUri, object extension);
member this.AddExtensionObject : string * obj -> unit
Public Sub AddExtensionObject (namespaceUri As String, extension As Object)

매개 변수

namespaceUri
String

개체에 연결할 네임스페이스 URI입니다. 기본 네임스페이스를 사용하려면 빈 문자열을 지정합니다.

extension
Object

목록에 추가할 개체입니다.

예외

namespaceUrinull 또는 http://www.w3.org/1999/XSL/Transform인 경우 namespaceUri에 이미 연결된 확장 개체가 있는 경우

호출자에게 이 메서드를 호출할 수 있는 권한이 없는 경우

예제

다음 예제에서 스타일시트는 XSLT 확장 개체를 사용하여 책 가격을 변환합니다.

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 stylesheet.
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("prices.xsl");

    // Create an XsltArgumentList.
    XsltArgumentList xslArg = new XsltArgumentList();

    // Add an object to calculate the new book price.
    BookPrice obj = new BookPrice();
    xslArg.AddExtensionObject("urn:price-conv", obj);

    using (XmlWriter w = XmlWriter.Create("output.xml"))
    {
        // Transform the file.
        xslt.Transform("books.xml", xslArg, w);
    }
  }

  // Convert the book price to a new price using the conversion factor.
  public class BookPrice{

    private decimal newprice = 0;
        
    public decimal NewPriceFunc(decimal price, decimal conv){
       decimal tmp = price*conv;
       newprice = decimal.Round(tmp, 2);
       return newprice;
    }
  }
}
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 stylesheet.
        Dim xslt As New XslCompiledTransform()
        xslt.Load("prices.xsl")
        
        ' Create an XsltArgumentList.
        Dim xslArg As New XsltArgumentList()
        
        ' Add an object to calculate the new book price.
        Dim obj As New BookPrice()
        xslArg.AddExtensionObject("urn:price-conv", obj)
        
        
        Using w As XmlWriter = XmlWriter.Create("output.xml")
            ' Transform the file.
            xslt.Transform("books.xml", xslArg, w)
        End Using


    
    End Sub
    
    ' Convert the book price to a new price using the conversion factor.    
    Public Class BookPrice
        
        Private newprice As Decimal = 0
                
        Public Function NewPriceFunc(ByVal price As Decimal, ByVal conv As Decimal) As Decimal 
            Dim tmp As Decimal = price * conv
            newprice = Decimal.Round(tmp, 2)
            Return newprice        
        End Function 'NewPriceFunc

    End Class

End Class

이 예제에서는 다음 데이터 파일을 입력으로 사용합니다.

books.xml

<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

prices.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myObj="urn:price-conv">

<!--Price conversion factor-->
<xsl:param name="conv" select="1.15"/>

  <xsl:template match="bookstore">
  <bookstore>
  <xsl:for-each select="book">
    <book>
    <xsl:copy-of select="node()"/>
       <new-price>
          <xsl:value-of select="myObj:NewPriceFunc(./price, $conv)"/>        
       </new-price>
    </book>
  </xsl:for-each>
  </bookstore>
  </xsl:template>
</xsl:stylesheet>

설명

params 지정되지 않은 개수의 매개 변수를 전달할 수 있는 키워드는 현재 지원되지 않습니다. 키워드로 params 정의된 메서드를 활용하는 XSLT 스타일시트가 제대로 작동하지 않습니다. 자세한 내용은 매개 변수를 참조하세요.

적용 대상

추가 정보