Solved

C# XSLT problem, parameter passing in not working

Posted on 2011-03-10
2
567 Views
Last Modified: 2013-12-17
I have a generic routing for passing parameters to an XSLT see code fragment 1
The test harness for is in code fragment 2

for some reason the parameter is not been passed to the XSLT

Can anyone help

FRAGMENT 1
 
public string Transform(string xslt,
                                string xmlBuffer,
                                string xsltParamsAsXml, 
                                string namespaceXML)
        {
            string functionReturnValue = null;

            try
            {

            XmlDocument xmlParamsDoc = new XmlDocument();

            xmlParamsDoc.LoadXml(xsltParamsAsXml);
            
            XsltArgumentList argsList = new XsltArgumentList(); 
            
            foreach (XmlNode xmlParam in xmlParamsDoc.SelectNodes("/Params/Param"))
            {
                string name = xmlParam.Attributes.GetNamedItem("name").InnerText;
                string value = xmlParam.Attributes.GetNamedItem("value").InnerText;
                argsList.AddParam(name, namespaceXML, value);
            }

                XmlDocument XMLDOM = new XmlDocument();
                
                XMLDOM.LoadXml(xmlBuffer);

                //' We create a new XslCompiledTransform object which supercedes the XslTransform class 
                XslCompiledTransform Transformer = new XslCompiledTransform();

                //XmlReader xmlReader = new XmlTextReader();

                //Convert to Bytes
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                Byte[] bytes = encoding.GetBytes(xslt);

                MemoryStream ms = new MemoryStream(bytes);
                Stream xlstStream = (Stream)ms;

                XmlReader xmlReader = XmlReader.Create(xlstStream);

                //' We have to load the XSL style sheet before we perform any transformation 
                Transformer.Load(xmlReader);

                //' We are setting the XmlWriterSettings so that we match the conformance level of the XmlReaderSettings 
                XmlWriterSettings myWriterSettings = new XmlWriterSettings();
                myWriterSettings.ConformanceLevel = ConformanceLevel.Fragment;

                //' Create a string builder which will eventually contain our transformed results 

                StringBuilder myString = new StringBuilder();
                //' Perform the transformation using the XmlDocument, the Parameters created from the Iterator and 
                //' write it to the StringBuilder 

                Transformer.Transform(XMLDOM, argsList, XmlWriter.Create(myString, myWriterSettings));

                functionReturnValue = myString.ToString();

            } /* throw */
            catch (Exception ex)
            {
                GenericLogger.ErrorLog(_userName, _machineName, ex.Message);

                throw new Exception("xmlFunctions:: Transform " + ex.Message);
            } /* catch */

            return functionReturnValue; 

        }

Open in new window




FRAGMENT 2
fileXLST = @"\\server2\batchXSLTDatabasesForRuleEngine.xslt";
xlstBuf = uF.ReadFileToString(fileXLST);
string ParamXML = "<Params><Param name='Environment' value='DEV'></Param></Params>";
xmlBuf = uF.ReadFileToString(@"\\SERVER2\XML\AlphaStartup02.xml");
string xmlTransformedDB = xlst.Transform(xlstBuf, xmlBuf, ParamXML, "");
xmlTransformedDB = xlst.FormatXml(xmlTransformedDB);

Open in new window

0
Comment
Question by:peter3244
  • 2
2 Comments
 

Accepted Solution

by:
peter3244 earned 0 total points
Comment Utility
Found problem

These lines ibn the XSLT file...

<xsl:template match="/">
<xsl:param name="Environment"></xsl:param>

 should be the other way around...

<xsl:param name="Environment"></xsl:param>
<xsl:template match="/">

Anyone know why?
0
 

Author Closing Comment

by:peter3244
Comment Utility
lines in XLST FILE WRONG way around...


MANY THANKS
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 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

9 Experts available now in Live!

Get 1:1 Help Now