12

我正在尝试将几个参数传递给 XSLT 样式表。我遵循了这个例子:Passing parameters to XSLT Stylesheet via .NET

但是我转换后的页面没有正确显示该值。

这是我的 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);
  }

这是我的 XSLT:

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

我错过了什么吗?

4

1 回答 1

16

这是我的 XSLT:

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

我错过了什么吗?

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

因此,代码必须是:

 <xsl:param name="processingId"/>     

 <xsl:template match="/">     
   ..HTML..     
   <xsl:value-of select="$processingId"/> 
   <!-- Possibly other processing here  -->
 </xsl:template>
于 2012-10-09T12:19:10.350 回答