XsltArgumentList.AddExtensionObject(String, Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
新增一個物件到中 XsltArgumentList ,並將其關聯到命名空間 URI。
public:
void AddExtensionObject(System::String ^ namespaceUri, System::Object ^ extension);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The stylesheet may have calls to methods of the extension object passed in which cannot be statically analyzed by the trimmer. Ensure all methods that may be called are preserved.")]
public void AddExtensionObject(string namespaceUri, object extension);
public void AddExtensionObject(string namespaceUri, object extension);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The stylesheet may have calls to methods of the extension object passed in which cannot be statically analyzed by the trimmer. Ensure all methods that may be called are preserved.")>]
member this.AddExtensionObject : string * obj -> unit
member this.AddExtensionObject : string * obj -> unit
Public Sub AddExtensionObject (namespaceUri As String, extension As Object)
參數
- namespaceUri
- String
用來關聯物件的命名空間 URI。 要使用預設命名空間,請指定一個空字串。
- extension
- Object
要加入清單的物品。
- 屬性
例外狀況
該 namespaceUri 是 null 或 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 樣式表無法正常運作。 欲了解更多資訊,請參閱參數。