Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameter to XSLT stylesheet

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?

like image 979
coson Avatar asked Oct 08 '12 19:10

coson


People also ask

How do you use parameters in XSLT?

To use an XSLT parameterCreate an XsltArgumentList object and add the parameter using the AddParam method. Call the parameter from the style sheet.

How do I pass multiple parameters in XSLT?

In order to pass multiple parameters to XSLT from BP you'll need to pass '=' as a separator.

Can you use CSS with XSLT?

Incorporating <STYLE> Elements into an XSLT FileAn XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.

What is difference between param and variable in XSLT?

The content model of both elements is the same. The way these elements declare variables is the same. However, the value of the variable declared using <xsl:param> is only a default that can be changed with the <xsl:with-param> element, while the <xsl:variable> value cannot be changed.


1 Answers

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>
like image 62
Dimitre Novatchev Avatar answered Oct 03 '22 23:10

Dimitre Novatchev