共用方式為


XSLT 參數

XSLT 參數可使用 AddParam 方法加入至 XsltArgumentList。 限定名稱與命名空間 URI 會在此時與參數物件產生關聯。

使用 XSLT 參數

  1. 建立 XsltArgumentList 物件,並使用 AddParam 方法加入參數。

  2. 從樣式表呼叫參數。

  3. XsltArgumentList 物件傳遞至 Transform 方法。

參數型別

參數物件應對應至 W3C 型別。 下表顯示對應的 W3C 型別、對等的 Microsoft .NET 類別 (型別),以及 W3C 型別是 XPath 型別還是 XSLT 型別。

W3C 型別

對等的 .NET 類別 (型別)

XPath 或 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

*這相當於含有單一節點的節點集。

如果參數物件不是上述其中一個類別,則會根據下列規則進行轉換。 Common Language Runtime (CLR) 數字型別會轉換為 DoubleDateTime 型別會轉換為 StringIXPathNavigable 型別會轉換為 XPathNavigatorXPathNavigator[] 會轉換為 XPathNodeIterator

所有其他型別都會擲回錯誤。

範例

下列範例將使用 AddParam 方法,建立保留計算折扣日期的參數。 折扣日期計算為從訂購日期起的 20 天。

Imports System
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
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();

  }

}

輸入

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>

輸出

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

請參閱

其他資源

XSLT 轉換