XSLT 参数

使用XsltArgumentList方法将 XSLT 参数添加到AddParam中。 此时,限定名称和命名空间 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

*这等效于包含单个节点的节点集。

如果参数对象不是上述类之一,则根据以下规则进行转换。 公共语言运行时 (CLR) 数值类型转换为 Double。 类型 DateTime 转换为 String. IXPathNavigable 类型转换为 XPathNavigator. XPathNavigator[] 转换为 XPathNodeIterator.

所有其他类型都引发错误。

示例:

以下示例使用 AddParam 该方法创建一个参数来保存计算的折扣日期。 折扣日期为订单日期后的第20天。

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

输入

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>  

另请参阅