Condividi tramite


Parametri XSLT

I parametri XSLT vengono aggiunti all'oggetto XsltArgumentList utilizzando il AddParam metodo . In quel momento, un nome qualificato e un URI dello spazio dei nomi sono associati all'oggetto parametro.

Per usare un parametro XSLT

  1. Creare un XsltArgumentList oggetto e aggiungere il parametro usando il AddParam metodo .

  2. Chiamare il parametro dal foglio di stile.

  3. Passare l'oggetto XsltArgumentList al Transform metodo .

Tipi di parametro

L'oggetto parametro deve corrispondere a un tipo W3C. La tabella seguente illustra i tipi W3C corrispondenti, le classi Microsoft .NET equivalenti (tipo) e se il tipo W3C è un tipo XPath o XSLT.

Tipo W3C Classe .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

*Equivale a un set di nodi che contiene un singolo nodo.

Se l'oggetto parametro non è una delle classi precedenti, viene convertito in base alle regole seguenti. I tipi numerici CLR (Common Language Runtime) vengono convertiti in Double. Il DateTime tipo viene convertito in String. IXPathNavigable i tipi vengono convertiti in XPathNavigator. XPathNavigator[] viene convertito in XPathNodeIterator.

Tutti gli altri tipi generano un errore.

Esempio

Nell'esempio seguente viene utilizzato il AddParam metodo per creare un parametro per contenere la data di sconto calcolata. La data di sconto viene calcolata come 20 giorni dalla data dell'ordine.

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

Inserimento

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>

Risultato

<?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>  

Vedere anche