How do I get an XSLT stylesheet to accept parameters

Source: Internet
Author: User
Tags xmlns xsl xslt

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,  "陈希章的报告");
            FileStream stream = new FileStream ("Test.htm", FileMode.Create);
            tran.Transform(doc.CreateNavigator(), a,  stream);
            stream.Close();
        }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.