Take the 2-minute tour ×

I have been familiar with passing Input Param to XSLT CompiledTransformation class, so that parser takes care of XSL file making use of Param in processing instruction provided in XSL file.

Is there a way where we can get output param (say a value of node or something else) from XSLT to host language like C#??

XslCompiledTransform xslTransform = new XslCompiledTransform();

string strXmlOutput = string.Empty;
StringWriter swXmlOutput = null;
MemoryStream objMemoryStream = null;
XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput);
swXmlOutput = new StringWriter();
objMemoryStream = new MemoryStream();

xslArg.AddParam("TESTING", "", SomeVar);
XsltSettings xslsettings = new XsltSettings(false, true);

xslTransform.Load(string_xslInput, xslsettings, new XmlUrlResolver());
xslTransform.Transform(xpathXmlOrig, xslArg, objMemoryStream);

This code indeed outputs transformed XML, but my question is can we take just one value as output param from XSL Tranformation (XSLT file)??

Something like this:

xslArg.OutputParam("testing"); //Something like this?
xslTransform.Transform(xpathXmlOrig, xslArg, objMemoryStream);
string outputparam = xslArg.GetParam("testing"); //ideal way of getting param after traformation!

Does XSLT provides scope for something like this?

share|improve this question
I think it is not possible. Maybe you can use extension class to call your own logic to pass value out of xslt, but it seems not to be a proper way. Why cant you parse result xslt? –  Jan Novák Jan 4 '13 at 15:19
XSLT is a functional language. Among other things this means that XSLT can't/doesn't modify an xsl:variable or an xsl:param. For a way to do what you want have a look here: xml.com/pub/a/2003/08/06/exslt.html –  Dimitre Novatchev Jan 4 '13 at 15:31
@DimitreNovatchev, hello there, it need not modify any param! I was expecting some output argument or property out of XSLT with which, XSL can communicate with host program! BTW, Flynn mentions about <xsl:message> ! I will try with that and come back with result.. thanks for responding! :) –  GeekWorking Jan 4 '13 at 17:55
@JanNovák, I wanted to save an additional effort and code in host program! If XML output is one thing I want, I also want to read a value of a node or a calculated value from XML in C# (after or before tranformation). XML-DOM plus XPath should do it for me using selectSingleNode but why do I need extra code, when XSLT is capable of that and I am already having Transformation thing in plate! (thinking to do in one shot) –  GeekWorking Jan 4 '13 at 17:58
@GeekWorking, You really need to read the technique described in the paper linked to in my first comment -- this is much more elegant and powerful than xsl:message –  Dimitre Novatchev Jan 4 '13 at 18:28

1 Answer 1

up vote 0 down vote accepted

In C#, your best bet is to put <xsl:message> instructions in your XSLT code, and hook into the XsltMessageEncountered event on the XsltArgumentList class.

You can test it's giving you the correct output without hooking the event, by watching the output of the app- in the absence of an event handler, messages are piped to standard output.

share|improve this answer
thank you.. so can I store the content of <xsl:message> into a string or somewhere somehow? –  GeekWorking Jan 4 '13 at 16:44
However you like. There's a Message property of type string on the event args, you can do whatever you need to do with that. –  Flynn1179 Jan 4 '13 at 16:48
that makes my day :) I will try it and accept your answer! Thanks for helping Flynn :) –  GeekWorking Jan 4 '13 at 17:53

Your Answer


