XSLT 字符串替换
我不太了解 XSL,但我需要修复此代码,我已减少它以使其更简单。
我收到这个错误
无效的 XSLT/XPath 函数
此行上的
<xsl:variable name="text" select="replace($text,'a','b')"/>
这是 XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:inm="http://www.inmagic.com/webpublisher/query" version="1.0">
<xsl:output method="text" encoding="UTF-8" />
<xsl:preserve-space elements="*" />
<xsl:template match="text()" />
<xsl:template match="mos">
<xsl:apply-templates />
<xsl:for-each select="mosObj">
'Notes or subject'
<xsl:call-template
name="rem-html">
<xsl:with-param name="text" select="SBS_ABSTRACT" />
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="rem-html">
<xsl:param name="text" />
<xsl:variable name="text" select="replace($text, 'a', 'b')" />
</xsl:template>
</xsl:stylesheet>
谁能告诉我它有什么问题吗?
I don't really know XSL but I need to fix this code, I have reduced it to make it simpler.
I am getting this error
Invalid XSLT/XPath function
on this line
<xsl:variable name="text" select="replace($text,'a','b')"/>
This is the XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:inm="http://www.inmagic.com/webpublisher/query" version="1.0">
<xsl:output method="text" encoding="UTF-8" />
<xsl:preserve-space elements="*" />
<xsl:template match="text()" />
<xsl:template match="mos">
<xsl:apply-templates />
<xsl:for-each select="mosObj">
'Notes or subject'
<xsl:call-template
name="rem-html">
<xsl:with-param name="text" select="SBS_ABSTRACT" />
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="rem-html">
<xsl:param name="text" />
<xsl:variable name="text" select="replace($text, 'a', 'b')" />
</xsl:template>
</xsl:stylesheet>
Can anyone tell me what's wrong with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
replace
不适用于 XSLT 1.0。Codesling 有一个 string-replace 模板 您可以用作函数的替代:
调用为:
另一方面,如果您实际上只需要用另一个字符替换一个字符,则可以调用
translate
具有类似的签名。像这样的东西应该可以正常工作:另外,请注意,在本示例中,我将变量名称更改为“newtext”,在 XSLT 中变量是不可变的,因此您不能执行与
$foo = $foo
等效的操作code> 就像您在原始代码中一样。replace
isn't available for XSLT 1.0.Codesling has a template for string-replace you can use as a substitute for the function:
invoked as:
On the other hand, if you literally only need to replace one character with another, you can call
translate
which has a similar signature. Something like this should work fine:Also, note, in this example, I changed the variable name to "newtext", in XSLT variables are immutable, so you can't do the equivalent of
$foo = $foo
like you had in your original code.下面是 XSLT 函数,其工作方式与 C# 的 String.Replace() 函数类似。
该模板有 3 个参数如下
text :- 您的主字符串
replace :- 您想要替换的字符串
by :- 字符串它将通过新字符串回复
下面是模板
下面的示例展示了如何调用它
您还可以参考 以下 URL 了解详细信息。
Here is the XSLT function which will work similar to the String.Replace() function of C#.
This template has the 3 Parameters as below
text :- your main string
replace :- the string which you want to replace
by :- the string which will reply by new string
Below are the Template
Below sample shows how to call it
You can also refer the below URL for the details.
我一直点击这个答案。但它们都没有列出 xsltproc(可能还有大多数 XSLT 1.0 处理器)的最简单解决方案:
I keep hitting this answer. But none of them list the easiest solution for xsltproc (and probably most XSLT 1.0 processors):
注意:如果您希望在需要替换源字符串中大量实例(例如长文本中的新行)的情况下使用已经提到的算法,则有 high< /em> 由于递归调用,您最终可能会遇到
StackOverflowException
。感谢 Xalan 的内置 Java 类型嵌入(没有了解如何在 Saxon 中做到这一点),我解决了这个问题:
Note: In case you wish to use the already-mentioned algo for cases where you need to replace huge number of instances in the source string (e.g. new lines in long text) there is high probability you'll end up with
StackOverflowException
because of the recursive call.I resolved this issue thanks to Xalan's (didn't look how to do it in Saxon) built-in Java type embedding:
当您的处理器在 .NET Framework(不支持 .NET Core 或 .NET 5)上运行或使用 MSXML(与基于 Java 的处理器或其他本机处理器相对)时,您可以使用以下代码。它使用
msxsl:script
。
确保将命名空间
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
添加到根xsl:stylesheet
或xsl:transform 元素。
此外,将
outlet
绑定到您喜欢的任何命名空间,例如xmlns:outlet = "http://my.functions"
。You can use the following code when your processor runs on .NET Framework (Not supported on .NET Core or .NET 5) or uses MSXML (as opposed to Java-based or other native processors). It uses
msxsl:script
.Make sure to add the namespace
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
to your rootxsl:stylesheet
orxsl:transform
element.In addition, bind
outlet
to any namespace you like, for instancexmlns:outlet = "http://my.functions"
.添加 case:
该例程非常好,但是它导致我的应用程序挂起,因此我需要在函数被递归调用之前
。我从这里得到了答案:
当测试挂在无限循环中
谢谢!
The rouine is pretty good, however it causes my app to hang, so I needed to add the case:
before the function gets called recursively.
I got the answer from here:
When test hanging in an infinite loop
Thank you!