XsltArgumentList.AddParam(String, String, Object) Método

Definição

Adiciona um parâmetro ao XsltArgumentList e associa-o ao nome qualificado do namespace.

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)

Parâmetros

name
String

O nome a associar ao parâmetro.

namespaceUri
String

O URI do namespace para associar ao parâmetro. Para usar o namespace por defeito, especifique uma string vazia.

parameter
Object

O valor do parâmetro ou objeto a adicionar à lista.

Exceções

O namespaceUri é ou null ou .http://www.w3.org/1999/XSL/Transform

O name nome não é válido segundo a especificação XML do W3C.

namespaceUri tem um parâmetro associado.

Exemplos

O exemplo seguinte utiliza o AddParam método para criar um parâmetro que representa a data e hora atuais.

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

O exemplo utiliza os dois ficheiros de dados seguintes como entrada.

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>

Observações

Devem parameter corresponder a um tipo W3C. A tabela seguinte mostra os tipos do W3C, seja XPath ou XSLT, e a respetiva classe .NET.

Tipo W3C Classe equivalente .NET (Tipo)
String (XPath) String
Boolean (XPath) Boolean
Number (XPath) Double
Result Tree Fragment (XSLT) XPathNavigator
Node Set (XPath) XPathNodeIterator

XPathNavigator[]
Node* (XPath) XPathNavigator

Isto equivale a um conjunto de nós que contém apenas um único nó.

Se o objeto de parâmetro invocado dentro da folha de estilos não for um dos acima, é convertido de acordo com as seguintes regras:

Todos os outros tipos geram um erro.

Aplica-se a

Ver também