如何讓xslt樣式表接受參數(shù)

字號:


    我們經(jīng)常會有這樣的需求:有多份數(shù)據(jù),需要共享一份樣式表來轉(zhuǎn)換。他們的 區(qū)別可能就在于頂部會有一些小的差異,那么如何解決這個事情呢?
    1. 在XSLT中定義參數(shù)
    <?xml version="1.0" encoding="utf- 8"?>
    <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"  exclude-result-prefixes="msxsl"
    >
    <xsl:output method="xml"  indent="yes"/>
    <xsl:param name="Title"></xsl:param>
    <xsl:template match="/">
    <html>
    <head></head>
    <body>
    <h1>
    <xsl:value-of  select="$Title"/>
    </h1>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    2. 在客戶端代碼中傳遞一個參數(shù)過來
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml.Xsl;
    using System.Xml.XPath;
    using System.Xml;
    using System.IO;
    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml ("<Tables><Table><Name>Orders</Name></T able></Tables>");
    XslCompiledTransform tran = new  XslCompiledTransform();
    tran.Load("Test.xslt");
    XsltArgumentList a = new XsltArgumentList ();
    a.AddParam("Title", string.Empty,  "陳希章的報告");
    FileStream stream = new FileStream ("Test.htm", FileMode.Create);
    tran.Transform(doc.CreateNavigator(), a,  stream);
    stream.Close();
    }
    }
    }