Share via


XsltArgumentList.AddParam(String, String, Object) 方法

定义

将参数添至 XsltArgumentList,并将其与命名空间限定名关联。

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

参数

name
String

要与参数关联的名称。

namespaceUri
String

与参数关联的命名空间 URI。 若要使用默认命名空间,请指定空字符串。

parameter
Object

要添至列表的参数值或对象。

例外

namespaceUrinullhttp://www.w3.org/1999/XSL/Transform

根据 W3C XML 规范,name 不是有效名称。

namespaceUri 已经具有与其关联的参数。

示例

以下示例使用 AddParam 方法创建表示当前日期和时间的参数。

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

public class Sample
{

    public static void Main()
    {

        // Create the XslCompiledTransform and load the stylesheet.
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load("order.xsl");

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

        // Create a parameter which represents the current date and time.
        DateTime d = DateTime.Now;
        xslArg.AddParam("date", "", d.ToString());

        // Transform the file.
        using (XmlWriter w = XmlWriter.Create("output.xml"))
        {
            xslt.Transform("order.xml", xslArg, w);
        }
    }
}
Imports System.IO
Imports System.Xml
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("order.xsl")

        ' Create the XsltArgumentList.
        Dim xslArg As New XsltArgumentList()

        ' Create a parameter which represents the current date and time.
        Dim d As DateTime = DateTime.Now
        xslArg.AddParam("date", "", d.ToString())

        Using w As XmlWriter = XmlWriter.Create("output.xml")
            ' Transform the file.
            xslt.Transform("order.xml", xslArg, w)
        End Using

    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>

order.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="date"/>
  <xsl:template match="/">
    <order>
      <date><xsl:value-of select="$date"/></date>
      <total><xsl:value-of select="sum(//price)"/></total>
    </order>
  </xsl:template>
</xsl:stylesheet>

注解

parameter 对应于 W3C 类型。 下表显示了 W3C 类型(XPath 或 XSLT)以及 corresponding.NET 类。

W3C 类型 Equivalent.NET 类 (类型)
String (XPath) String
Boolean (XPath) Boolean
Number (XPath) Double
Result Tree Fragment (XSLT) XPathNavigator
Node Set (XPath) XPathNodeIterator

XPathNavigator[]
Node* (XPath) XPathNavigator

*等效于包含单个节点的节点集。

如果从样式表中调用的参数对象不是上述对象之一,则会根据以下规则对其进行转换:

所有其他类型均将引发错误。

适用于

另请参阅