[Last Call] Learn how to a build a cloud-first strategyRegister Now


How xsl:template match get the param?

Posted on 2009-12-24
Medium Priority
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 asp.net, c#, xslt 1.0      
Question by:techques
  • 2
  • 2
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 800 total points
ID: 26118852
<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]">...

Accepted Solution

miggety earned 1200 total points
ID: 26128335
Here is my xsl file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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

ID: 26138971
Both are great solution!
LVL 60

Expert Comment

by:Geert Bormans
ID: 26139370

Expert Comment

ID: 26139667
Thanks Gertone!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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)
Suggested Courses

830 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