Parametry XSLT

Parametry XSLT są dodawane do XsltArgumentList metody przy użyciu AddParam metody . Kwalifikowana nazwa i identyfikator URI przestrzeni nazw są skojarzone z obiektem parametrów w tym czasie.

Aby użyć parametru XSLT

  1. XsltArgumentList Utwórz obiekt i dodaj parametr przy użyciu AddParam metody .

  2. Wywołaj parametr z arkusza stylów.

  3. XsltArgumentList Przekaż obiekt do Transform metody .

Typy parametrów

Obiekt parametru powinien odpowiadać typowi W3C. W poniższej tabeli przedstawiono odpowiadające im typy W3C, równoważne klasy microsoft .NET (typ) oraz to, czy typ W3C jest typem XPath, czy typem XSLT.

Typ W3C Równoważna klasa .NET (typ) Typ XPath lub 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

*Jest to odpowiednik zestawu węzłów, który zawiera jeden węzeł.

Jeśli obiekt parametru nie jest jedną z powyższych klas, jest konwertowany zgodnie z następującymi regułami. Typy liczbowe środowiska uruchomieniowego języka wspólnego (CLR) są konwertowane na Double. Typ DateTime jest konwertowany na String. IXPathNavigable typy są konwertowane na XPathNavigator. Klasa XPathNavigator[] jest konwertowana na XPathNodeIterator.

Wszystkie inne typy zgłaszają błąd.

Przykład

W poniższym przykładzie użyto metody do utworzenia parametru AddParam do przechowywania daty obliczonego rabatu. Data rabatu jest obliczana na 20 dni od daty zamówienia.

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

Dane wejściowe

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>

Wynik

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

Zobacz też