C# 将参数传递给 XSLT 样式表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12787862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:28:07  来源:igfitidea点击:

Pass parameter to XSLT stylesheet

c#xslttransformation

提问by coson

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.

我正在尝试将几个参数传递给 XSLT 样式表。我遵循了这个例子:通过 .NET 将参数传递给 XSLT 样式表

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.

这是我的 C# 代码。我不得不添加自定义函数,因为Visual Studio 2010中不使用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:

这是我的 XSLT:

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

Am I missing something?

我错过了什么吗?

采纳答案by Dimitre Novatchev

Here is my XSLT:

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

Am I missing something?

这是我的 XSLT:

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

我错过了什么吗?

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

是的,您忽略了一个事实,即 XSLT 转换的调用者可以设置全局级参数的值——而不是模板级参数的值。

Therefore, the code must be:

因此,代码必须是:

 <xsl:param name="processingId"/>     

 <xsl:template match="/">     
   ..HTML..     
   <xsl:value-of select="$processingId"/> 
   <!-- Possibly other processing here  -->
 </xsl:template>