将参数传递给XSLT样式表

cos*_*son 11 c# xslt transformation

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

但我的转换页面没有正确显示值.

这是我的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);
  }
Run Code Online (Sandbox Code Playgroud)

这是我的XSLT:

    <xsl:template match="/">
    <xsl:param name="processingId"></xsl:param>
    ..HTML..
    <xsl:value-of select="$processingId"/>
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Dim*_*hev 15

这是我的XSLT:

<xsl:template match="/">     
  <xsl:param name="processingId"></xsl:param>     
  ..HTML..     
  <xsl:value-of select="$processingId"/> 
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

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

因此,代码必须是:

 <xsl:param name="processingId"/>     

 <xsl:template match="/">     
   ..HTML..     
   <xsl:value-of select="$processingId"/> 
   <!-- Possibly other processing here  -->
 </xsl:template>
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是一个非常方便的事情,任何人都知道这是否也可以使用Java完成? (2认同)