强制xslt使用版本2和xslCompiledTransform

我有以下xslt,我需要使用像’format-date’这样的xslt版本2.0函数。 如何使用XsltCompiledTransform类(c#,。net 4.5)声明Xsl表使用2.0版。

       table{border-collapse:collapse;font-family:"Verdana";} table,td{border:1px solid black;color:black; background-color:white;font-family:"Verdana";} table,th{border:1px solid balck;background-color:black;color:white;font-family:"Verdana"; } .rt{color:red;font-family:"Verdana";} .nt{color:black;font-family:"Verdana";} .redb{color:yellow; background-color:red;font-family:"Verdana";} .greenb{color:white;background-color:green;font-family:"Verdana";} .blackb{color:white;background-color:black;font-family:"Verdana";}  EDI validation Result    

EDI validation result of the PO received from .

Position Item Code UoM Ordered Qty . Ship Request Net-Quoted Net-Catalog Status
OK

The order validation has failed, The order will not be processesed as there are lines in error.

The PO is rejected as per agreed processing rules.

The Order validated succesfully. Will e-mail Order Acknoledgement (non-edi) shortly.

微软的XslCompiledTransform是一个XSLT 1.0处理器。 要在.NET中使用XSLT 2.0,您有两个第三方选项,来自http://saxon.sourceforge.net/或XmlPrime的.NET版Saxon 9。 Saxon有一个开源版本HE和两个商业版本PE和EE,XmlPrime用于商业用途需要许可证。

正如Martin写的那样,微软的处理器仅支持1.0 – 即使是现在,在2016年。我遇到了类似的问题(我需要在我的XSLT中使用正则表达式)并且我通过使用内联C#来完成工作来解决它。

我的解决方案基于此示例: XSLT样式表脚本使用(MSDN)

您必须向xsl:stylesheet节点添加一些内容。 我看起来像:

  

然后,您将定义您的脚本。 我是这样的:

     

然后我能够在我的XSLT中调用该函数:

  

在教程页面上没有提到,但是我在服务器上处理XSLT的地方,我还必须创建一个XsltSettings对象来启用脚本执行:

 XsltSettings settings = new XsltSettings(false, true); // enable script execution XsltCompiledTransform transform = new XslCompiledTransform(); transform.Load("template.xsl", settings, new XmlUrlResolver()); 

当然,请考虑安全性 – 如果您允许执行任意C#脚本,请确保您的XSLT文件仅包含受信任和/或已清理的输入。

作为批准答案的附录,这里是@TonyP对使用微软format-date的评论的扩展。

格式日期的MSDN文档: https : //msdn.microsoft.com/en-us/library/ms256099%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

     

注意:MS的格式化日期选项远不如XSLT 2.0中提供的选项丰富(参见文档); 所以我相信d MMMM yyyy格式是您可以获得的最接近的格式。