Pass parameter to XSLT stylesheet

你。 提交于 2019-12-30 17:23:57

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/12787862/pass-parameter-to-xslt-stylesheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!