We often have this requirement: there are multiple copies of data that need to be shared in a stylesheet to convert. The difference may be that there are some small differences at the top, so how do you solve it?
1. Define parameters in 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= "/" >
<body>
<xsl:value-of select= "$Title"/>
</body>
</xsl:template>
</xsl:stylesheet>
2. Pass a parameter in the client code come on.
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, "Chen Xizhang's report");
FileStream stream = new FileStream ("test.htm", FileMode.Create);
Tran. Transform (Doc. CreateNavigator (), A, stream);
Stream. Close ();
}
}
}