12

I'm trying to pass a couple of parameters to an XSLT style sheet. I have followed the example: Passing parameters to XSLT Stylesheet via .NET.

But my transformed page is not correctly displaying the value.

Here is my C# code. I had to add a custom function to perform some arithmetic because Visual Studio 2010 doesn't use XSLT 2.0.

  var args = new XsltArgumentList();
  args.AddExtensionObject("urn:XslFunctionExtensions", new XslFunctionExtensions());
  args.AddParam("processingId", string.Empty, processingId);

  var myXPathDoc = new XPathDocument(claimDataStream);
  var xslCompiledTransformation = new XslCompiledTransform(true);

  // XSLT File
  xslCompiledTransformation.Load(xmlReader);

  // HTML File
  using (var xmlTextWriter = new XmlTextWriter(outputFile, null))
  {
      xslCompiledTransformation.Transform(myXPathDoc, args, xmlTextWriter);
  }

Here is my XSLT:

    <xsl:template match="/">
    <xsl:param name="processingId"></xsl:param>
    ..HTML..
    <xsl:value-of select="$processingId"/>

Am I missing something?

Martijn
  • 11,964
  • 12
  • 50
  • 96
coson
  • 8,301
  • 15
  • 59
  • 84
  • Do you have `` at the top level? Seems that you define your parameter within a template which will create a local parameter. For external parameters the parameter must be global so it has to be defined at the same level as xsl:template elements – Pawel Oct 08 '12 at 21:01
  • I'm not sure what you mean at the top level. The parameter definition is below the as I pasted in my question. Above that is the tag. – coson Oct 08 '12 at 22:09
  • I meant it should not be inside xsl:template. Try something like this: ` ...` – Pawel Oct 08 '12 at 22:43

1 Answers1

16

Here is my XSLT:

<xsl:template match="/">     
  <xsl:param name="processingId"></xsl:param>     
  ..HTML..     
  <xsl:value-of select="$processingId"/> 

Am I missing something?

Yes, you are missing the fact that the invoker of an XSLT transformation can set the values of global-level parameters -- not the values of template-level parameters.

Therefore, the code must be:

 <xsl:param name="processingId"/>     

 <xsl:template match="/">     
   ..HTML..     
   <xsl:value-of select="$processingId"/> 
   <!-- Possibly other processing here  -->
 </xsl:template>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • 2
    This is actually a pretty handy thing to know, anyone know if this can also be done using Java? – adam5990 May 13 '15 at 18:12
  • 1
    @adam5990, Yes, in case the particular XSLT processor used is written in Java. The way to specify programmatically global parameters' values is implementation-specific and varies from XSLT-processor to XSLT-processor. See for example this section of the Saxon documentation: saxonica.com/documentation/#!using-xsl/embedding/… . In particular it says (under point 1.): "You can use methods on the XsltTransformer to set values for global stylesheet parameters" Do note, though, that it is much easier to do this manually, invokinf an XSLT transformation from the command-line. – Dimitre Novatchev May 13 '15 at 19:29