How xsl:template match get the param?

Posted on 2009-12-24
Last Modified: 2013-11-18
<xsl:param name="UserName" />
<xsl:template match="/UserInfo/User[@Name=$UserName]/Account">

I pass the param from server side to xsl, but it has error.

I use, c#, xslt 1.0      
Question by:techques
    LVL 60

    Assisted Solution

    by:Geert Bormans
    <xsl:template match="/UserInfo/User[@Name=$UserName]/Account">

    it is not allowed to have a parameter in a match
    (match should be static at compile time)

    There are two options.

    - be selectiev when you apply for the template
    <xsl:apply-templates select="/UserInfo/User[@Name=$UserName]/Account"/>

    - or use a test in the template for Account

    <xsl:template match="/UserInfo/User/Account">
    <xsl:if test="ancestor::User[@Name=$UserName]">...
    LVL 2

    Accepted Solution

    Here is my xsl file:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="" version="1.0" xmlns:dm="urn:dmca-schema">
    <xsl:output method="xml" encoding="UTF-8"/>
    <xsl:param name="statusid"/>
    <xsl:template match="/">
          <xsl:for-each select="/dm:PaymentBatch/dm:Collection/dm:Item">
                <paymentid><xsl:value-of select="dm:PaymentID"/></paymentid>
                <statusid><xsl:value-of select="$statusid"/></statusid>

    and here is the code from the function where i transform my XML using the xsl file and pass in a parameter

    string validation = ConfigurationManager.AppSettings["validation"].ToString();
                string transformation = ConfigurationManager.AppSettings["transformation"].ToString(); //here i'm pulling in the path to my xsl file.

                XmlSchemaSet sc = new XmlSchemaSet();
                sc.Add("urn:dmca-schema", validation);

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas = sc;
                settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                settings.IgnoreWhitespace = true;
                settings.IgnoreComments = true;

                TextWriter t = new StringWriter();
                XmlSerializer serializer = new XmlSerializer(typeof(PaymentBatch));
                serializer.Serialize(t, paymentbatch);
                XmlReader reader1 = XmlReader.Create(new StringReader(t.ToString()), settings);
                XmlReader reader2 = XmlReader.Create(transformation);

                StringBuilder transform = new StringBuilder();
                XmlWriter xmlwriter = XmlWriter.Create(transform);

                string param = ConfigurationManager.AppSettings["param"].ToString();  //Here i pull the parameter from the config file
                string ns = ConfigurationManager.AppSettings["namespace"].ToString();

                XsltArgumentList xslArg = new XsltArgumentList();
                xslArg.AddParam(param, ns, ConvertStatus(status));  //IMPORTANT this is the call used to create the parameter!!

                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Transform(reader1, xslArg, xmlwriter);


    Let me know if have any questions about the code.

                return transform.ToString();

    Author Comment

    Both are great solution!
    LVL 60

    Expert Comment

    by:Geert Bormans
    LVL 2

    Expert Comment

    Thanks Gertone!

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System ( introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now