xml 使用 XPath 函数 fn:replace from XSLT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2183908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using XPath function fn:replace from XSLT
提问by larsmoa
I'm trying to use XSLT to convert a simple XML schema to HTML and planned to use fn:replaceto replace returns (\n) with <p>;. However, I can't figure out how to use this function properly.
我正在尝试使用 XSLT 将一个简单的 XML 模式转换为 HTML,并计划fn:replace用于将返回 ( \n)替换为<p>;. 但是,我无法弄清楚如何正确使用此功能。
A simplified version of the XSLT I'm using reads:
我正在使用的 XSLT 的简化版本如下:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="/root">
<html>
<body>
<!-- Replace \n with <p> -->
<xsl:value-of select="fn:replace(value, '\n', '<p>')" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
And the input to this XLST is e.g.:
这个 XLST 的输入是例如:
<?xml version="1.0"?>
<root>
<value><![CDATA[
Hello
world!
]]></value>
</root>
The conversion fails on fn:replacewith an NoSuchMethodException. If I change the replace statement to
转换失败fn:replace并出现 NoSuchMethodException。如果我将替换语句更改为
<xsl:value-of select="fn:replace('somestring', '\n', '<p>')" />
I get an IllegalArgumentException. How do I use fn:replaceto achieve what I want?
我得到一个 IllegalArgumentException。我如何使用fn:replace来实现我想要的?
I'm using Butterfly XML Editorto test the XSLT.
我正在使用Butterfly XML Editor来测试 XSLT。
采纳答案by Martin Honnen
XPath 2.0 has a replace function that you can use with any XSLT 2.0 processor like Saxon 9 or AltovaXML tools or Gestalt. You seem to try to use the function with an XSLT 1.0 processor, that is not going to work. In case you are restricted to an XSLT 1.0 processor you will need to implement the replacement with a named recursive template or with the help of an extension.
XPath 2.0 具有替换功能,您可以将其与任何 XSLT 2.0 处理器(如 Saxon 9 或 AltovaXML 工具或格式塔)一起使用。您似乎尝试将该函数与 XSLT 1.0 处理器一起使用,但这是行不通的。如果您受限于 XSLT 1.0 处理器,您将需要使用命名递归模板或借助扩展来实现替换。
However note that even with XSLT 2.0 your attempt to use replace seems wrong as you will produce a text node with a 'p' tag markup while I assume you want to create a 'p' element node in the result. So even with XSLT 2.0 using analyze-string instead of replace is more likely to get you the result you want.
但是请注意,即使使用 XSLT 2.0,您尝试使用 replace 似乎是错误的,因为您将生成带有 'p' 标记标记的文本节点,而我假设您想在结果中创建一个 'p' 元素节点。因此,即使使用分析字符串而不是替换的 XSLT 2.0 也更有可能获得您想要的结果。
回答by larsmoa
By bumping the version attribute of <xsl:stylesheet>to 2.0 and using
通过将 version 属性提高<xsl:stylesheet>到 2.0 并使用
<xsl:value-of select="replace(description, '\n', '<p/>')" disable-output-escaping="yes" />
<xsl:value-of select="replace(description, '\n', '<p/>')" disable-output-escaping="yes" />
I was able to make the replace work.
我能够使替换工作。

