Compartir a través de


Parámetros XSLT

Los parámetros XSLT se agregan al XsltArgumentList mediante el AddParam método . Un nombre calificado y un URI de espacio de nombres están asociados al objeto de parámetro en ese momento.

Para usar un parámetro XSLT

  1. Cree un XsltArgumentList objeto y agregue el parámetro mediante el AddParam método .

  2. Llame al parámetro desde la hoja de estilos.

  3. Pase el XsltArgumentList objeto al Transform método .

Tipos de parámetros

El objeto de parámetro debe corresponder a un tipo W3C. En la tabla siguiente se muestran los tipos W3C correspondientes, las clases equivalentes de Microsoft .NET (tipo) y si el tipo W3C es un tipo XPath o un tipo XSLT.

Tipo W3C Clase .NET equivalente (tipo) Tipo XPath o XSLT
String System.String XPath
Boolean System.Boolean XPath
Number System.Double XPath
Result Tree Fragment System.Xml.XPath.XPathNavigator XSLT
Node* System.Xml.XPath.XPathNavigator XPath
Node Set XPathNodeIterator

XPathNavigator[]
XPath

*Esto equivale a un conjunto de nodos que contiene un único nodo.

Si el objeto de parámetro no es una de las clases anteriores, se convierte según las reglas siguientes. Los tipos numéricos de Common Language Runtime (CLR) se convierten en Double. El DateTime tipo se convierte en String. IXPathNavigable Los tipos se convierten en XPathNavigator. XPathNavigator[] se convierte en XPathNodeIterator.

El resto de los tipos inician un error.

Ejemplo

En el ejemplo siguiente se usa el AddParam método para crear un parámetro para contener la fecha de descuento calculada. La fecha de descuento se calcula que es de 20 días a partir de la fecha de pedido.

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 style sheet.
      XslCompiledTransform xslt = new XslCompiledTransform();
      xslt.Load("discount.xsl");

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

      // Calculate the discount date.
      DateTime orderDate = new DateTime(2004, 01, 15);
      DateTime discountDate = orderDate.AddDays(20);
      argList.AddParam("discount", "", discountDate.ToString());

      // Create an XmlWriter to write the output.
     XmlWriter writer = XmlWriter.Create("orderOut.xml");

     // Transform the file.
     xslt.Transform(new XPathDocument("order.xml"), argList, writer);
     writer.Close();
  }
}
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 style sheet.
        Dim xslt as XslCompiledTransform = new XslCompiledTransform()
        xslt.Load("discount.xsl")

        ' Create the XsltArgumentList.
        Dim argList as XsltArgumentList = new XsltArgumentList()

        ' Calculate the discount date.
        Dim orderDate as DateTime = new DateTime(2004, 01, 15)
        Dim discountDate as DateTime = orderDate.AddDays(20)
        argList.AddParam("discount", "", discountDate.ToString())

        ' Create an XmlWriter to write the output.             
        Dim writer as XmlWriter = XmlWriter.Create("orderOut.xml")

        ' Transform the file.
        xslt.Transform(new XPathDocument("order.xml"), argList, writer)
        writer.Close()

    end sub

end class

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>
discount.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="discount"/>
  <xsl:template match="/">
    <order>
      <xsl:variable name="sub-total" select="sum(//price)"/>
      <total><xsl:value-of select="$sub-total"/></total>
           15% discount if paid by: <xsl:value-of select="$discount"/>
     </order>
  </xsl:template>
</xsl:stylesheet>

Salida

<?xml version="1.0" encoding="utf-8"?>  
<order>  
  <total>36.9</total>  
     15% discount if paid by: 2/4/2004 12:00:00 AM  
</order>  

Consulte también