您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

如何让xslt样式表接受参数

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 我们经常会有这样的需求:有多份数据,需要共享一份样式表来转换。他们的 区别可能就在于顶部会有一些小的差异,那么如何解决这个事情呢?

1. 在XSLT中定义参数

<?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. 在客户端代码中传递一个参数过来

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();
}
}
}