9

I'm debugging a transform with Visual Studio. The application that uses the transform normally also passes in some parameters:

XslTransform xslTransform = new XslTransform();
xslTransform.Load(myXslt);
XsltArgumentList transformArgumentList = new XsltArgumentList();
transformArgumentList.AddParam(paramName1, String.Empty, paramValue1); // this
transformArgumentList.AddParam(paramName2, String.Empty, paramValue2); // and this
xslTransform.Transform(inputStream, transformArgumentList, outputStream);

How can I set the parameters when debugging?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Justin R.
  • 23,435
  • 23
  • 108
  • 157

1 Answers1

7

How can I set the parameters when debugging?

You should use the following XslCompiledTransform constructor:

public XslCompiledTransform(
    bool enableDebug
)

with the enableDebug argument set to true.

Then you can start debugging and the debugger will stop on breakpoints set in your XSLT transformation.

Here is an example:

// Enable XSLT debugging.
XslCompiledTransform xslt = new XslCompiledTransform(true);

// Load the style sheet.
xslt.Load("MyTransformation.xsl");

// Create the writer.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent=true;
XmlWriter writer = XmlWriter.Create("output.xml", settings);

// Execute the transformation.
xslt.Transform("books.xml", writer);
writer.Close();

Of course, if you are lazy, you may just hardcode the values of the parameters in your XSLT stylesheet:

<xsl:param name="param1" select="SomeValue1"/>
<xsl:param name="param2" select="SomeValue2"/>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Is this the only way to do this? I'd rather not have to run the entire app each time, as there is quite a lot of work done before the transform. – Justin R. Oct 09 '10 at 20:44
  • @fatcat1111: if you don't want to hardcode the `` s in your XSLT code, then this is the only way to debug and pass parameters. Otherwise, you may hardcode the params: `' – Dimitre Novatchev Oct 09 '10 at 20:52
  • Shouldn't the select value have `''` wrapped around it e.g. ``? – Malachi Apr 16 '13 at 14:52
  • 1
    @Malachi Yes, but then this will pass as value of the parameter, the string (*literally*) `'SomeValue2'` . We all understand that the generic names used in this code actually mean: Put here your specific value. This specific value maybe a string constant -- then surrounding quotes are necessary, but it may also be a number or a Boolean or an expression, and in all of these cases there must not be any surrounding quotes. – Dimitre Novatchev Apr 16 '13 at 16:05
  • @DimitreNovatchev Thanks for the clarification Dimitre. I've been learning some XSLT today and your post came up and helped me out :) – Malachi Apr 16 '13 at 20:59
  • Just to maybe help clarify Justin's concern, the hardcoded values act as defaults and will allow you to debug your stylesheet using the VS xml debugger. You can supply your 'actual' values at run time and these default values will be over-ridden. – Rik Bradley Aug 05 '21 at 15:25