gpt4 book ai didi

c# - 强制 xslt 使用带有 xslCompiledTransform 的版本 2

转载 作者:行者123 更新时间:2023-11-30 13:37:01 25 4
gpt4 key购买 nike

我有以下 xslt ,我需要使用 xslt 2.0 版功能,如“格式日期”。如何使用 XsltCompiledTransform 类(c#、.net 4.5)声明 Xsl 工作表使用 2.0 版。

 <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/xhtml1/strict"
xmlns:msxsl='urn:schemas-microsoft-com:xslt'
xmlns:var='urn:var'
xmlns:JS='urn:JS'
>
<xsl:output method="html"/>
<xsl:variable name="n" select="1"/>
<xsl:template match="/NewDataSet">
<html>
<head>
<style>
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";}
</style>
<title>EDI validation Result </title>
</head>
<body>
<p class="nt">
EDI validation result of the PO <span class="rt"><xsl:value-of select="info/pono"/></span>
received from <xsl:value-of select="info/CustomerName"/>.
</p>

<table>
<th class="blackb" >Position</th>
<th class="blackb"> Item Code </th>
<th class="blackb">UoM</th>
<th class="blackb"> Ordered Qty .</th>
<th class="blackb">Ship Request</th>
<th class="blackb"> Net-Quoted </th>
<th class="blackb"> Net-Catalog </th>
<th class="blackb">Status</th>
<xsl:for-each select="Table">
<tr>
<xsl:choose>
<xsl:when test="Status !=''">
<xsl:value-of disable-output-escaping="yes" select="JS:IncBlines()"/>
<td class="redb"><xsl:value-of select="Position"/></td>
<td class="redb"><xsl:value-of select="ItemCode "/></td>
<td class="redb"><xsl:value-of select="UoM"/></td>
<td class="redb"><xsl:value-of select="QtyOrdered"/></td>
<td class="redb"><xsl:value-of select="format-date(RequiredBy,'D1o [MNn] [Y0001]')"/></td>
<td class="redb"><xsl:value-of select="PriceQuoted"/></td>
<td class="redb"><xsl:value-of select="Net"/></td>
<td class="redb"><xsl:value-of select="Status"/></td>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="JS:IncGlines()"/>

<td class="greenb"><xsl:value-of select="Position"/></td>
<td class="greenb"><xsl:value-of select="ItemCode"/></td>
<td class="greenb"><xsl:value-of select="UoM"/></td>
<td class="greenb"><xsl:value-of select="QtyOrdered"/></td>
<td class="greenb"><xsl:value-of select="format-date(RequiredBy,'D1o [MNn] [Y0001]')"/></td>


<td class="greenb"><xsl:value-of select="PriceQuoted"/></td>
<td class="greenb"><xsl:value-of select="Net"/></td>
<td class="greenb"><xsl:value-of select="Status"/>OK</td>

</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>

<xsl:if test="JS:GetBlines() &gt; 0" >

<p class="nt">

The order validation has failed,

The order will not be processesed as there are <xsl:value-of select ="JS:GetBlines()"/> lines in error.
<p class="rt">

The P.O is rejected as per agreed processing rules.


</p>
</p>

</xsl:if>

<xsl:if test="JS:GetBlines() &lt; 1">
<p class="nt">
The Order validated succesfully.
Will e-mail Order Acknoledgement (non-edi) shortly.
</p>
</xsl:if>

</body>
</html>
</xsl:template>
<msxsl:script language='JScript' implements-prefix='JS'>

<![CDATA[
var j :int=0;
var blines:int =0;
var glines:int=0;
function Inc(current)
{j=j+current;

return j+current;
}
function IncBlines()
{
blines++;
}
function IncGlines()
{
glines++;
}

function GetBlines()
{
return blines;
}
function GetGlines()
{
return glines;
}
]]>
</msxsl:script>
</xsl:stylesheet>

最佳答案

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

我的解决方案基于这个例子: XSLT Stylesheet Scripting Using <msxsl:script> (MSDN)

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

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
exclude-result-prefixes="msxsl">

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

<!-- Script to check for URLs in values -->
<msxsl:script language="C#" implements-prefix="user">
<![CDATA[
public string ExtractUrl(string str)
{
return Regex.Match(str, @"(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))").Value;
}
]]>
</msxsl:script>

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

<xsl:variable name="extractedUrl" select="user:ExtractUrl(.)"></xsl:variable>

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

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

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

关于c# - 强制 xslt 使用带有 xslCompiledTransform 的版本 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27020359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com