XsltArgumentList.AddExtensionObject-Methode
Fügt der XsltArgumentList ein neues Objekt hinzu und ordnet es dem Namespace-URI zu.
Namespace: System.Xml.Xsl
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Sub AddExtensionObject ( _
namespaceUri As String, _
extension As Object _
)
'Usage
Dim instance As XsltArgumentList
Dim namespaceUri As String
Dim extension As Object
instance.AddExtensionObject(namespaceUri, extension)
public void AddExtensionObject (
string namespaceUri,
Object extension
)
public:
void AddExtensionObject (
String^ namespaceUri,
Object^ extension
)
public void AddExtensionObject (
String namespaceUri,
Object extension
)
public function AddExtensionObject (
namespaceUri : String,
extension : Object
)
Parameter
- namespaceUri
Der Namespace-URI, der dem Objekt zugeordnet werden soll. Um den Standardnamespace verwenden zu können, geben Sie eine leere Zeichenfolge an.
- extension
Das Objekt, das der Liste hinzugefügt werden soll.
Ausnahmen
Ausnahmetyp | Bedingung |
---|---|
Der namespaceUri ist entweder NULL (Nothing in Visual Basic) oder http://www.w3.org/1999/XSL/Transform. Dem namespaceUri ist bereits ein Erweiterungsobjekt zugeordnet. |
|
Der Aufrufer verfügt nicht über ausreichende Berechtigungen zum Aufrufen dieser Methode. |
Hinweise
Das params
-Schlüsselwort, das die Übergabe einer nicht festgelegten Anzahl von Parametern ermöglicht, wird derzeit nicht unterstützt. XSLT-Stylesheets, die durch das params
-Schlüsselwort definierte Methoden verwenden, funktionieren nicht ordnungsgemäß. Weitere Informationen finden Sie unter params (C#-Referenz).
Hinweise für Aufrufer Zum Aufrufen dieser Methode ist FullTrust erforderlich. Weitere Informationen finden Sie unter Codezugriffssicherheit.
Beispiel
Im folgenden Beispiel konvertiert das Stylesheet den Buchpreis mithilfe eines XSLT-Erweiterungsobjekts.
Imports System
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")
' Load the XML data file.
Dim doc As New XPathDocument("books.xml")
' 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)
' Transform the file.
xslt.Transform("books.xml", xslArg, XmlWriter.Create("output.xml"))
End Sub 'Main
' 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 'BookPrice
End Class 'Sample
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");
// Load the XML data file.
XPathDocument doc = new XPathDocument("books.xml");
// 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);
// Transform the file.
xslt.Transform("books.xml", xslArg, XmlWriter.Create("output.xml"));
}
// 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;
}
}
}
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::XPath;
using namespace System::Xml::Xsl;
// Convert the book price to a new price using the conversion factor.
public ref class BookPrice
{
private:
Decimal newprice;
public:
BookPrice()
{
newprice = 0;
}
Decimal NewPriceFunc( Decimal price, Decimal conv )
{
Decimal tmp = price * conv;
newprice = Decimal::Round( tmp, 2 );
return newprice;
}
};
public ref class Sample
{
public:
Sample()
{
// Create the XslCompiledTransform and load the stylesheet.
XslCompiledTransform^ xslt = gcnew XslCompiledTransform;
xslt->Load( "prices.xsl" );
// Load the XML data file.
XPathDocument^ doc = gcnew XPathDocument( "books.xml" );
// Create an XsltArgumentList.
XsltArgumentList^ xslArg = gcnew XsltArgumentList;
// Add an object to convert the book price.
BookPrice^ obj = gcnew BookPrice;
xslArg->AddExtensionObject( "urn:price-conv", obj );
// Transform the file.
xslt->Transform(doc, xslArg, XmlWriter::Create("output.xml"));
}
};
int main()
{
Sample^ test = gcnew Sample;
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.XPath.*;
import System.Xml.Xsl.*;
public class Sample
{
public static void main(String[] args)
{
Sample test = new Sample();
} //main
public Sample()
{
// Create the XslCompiledTransform and load the stylesheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("prices.xsl");
// Load the XML data file.
XPathDocument doc = new XPathDocument("books.xml");
// Create an XsltArgumentList.
XsltArgumentList xslArg = new XsltArgumentList();
// Add an object to convert the book price.
BookPrice obj = new BookPrice();
xslArg.AddExtensionObject("urn:price-conv", obj);
// Transform the file.
xslt.Transform("books.xml", xslArg, XmlWriter.Create("output.xml"));
} // Sample
// Convert the book price to a new price using the conversion factor.
public class BookPrice
{
private System.Decimal newprice = System.Convert.ToDecimal(0);
public System.Decimal NewPriceFunc(System.Decimal price,
System.Decimal conv)
{
System.Decimal tmp = Decimal.Multiply(price, conv);
newprice = System.Decimal.Round(tmp, 2);
return newprice;
} //NewPriceFunc
} //BookPrice
} //Sample
Im Beispiel werden die folgenden Datendateien als Eingabe verwendet.
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>
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
XsltArgumentList-Klasse
XsltArgumentList-Member
System.Xml.Xsl-Namespace
Weitere Ressourcen
XSLT-Erweiterungsobjekte
XSLT-Parameter
Verwenden der XslCompiledTransform-Klasse