使用 XPath 函数 fn:replace from XSLT [英] Using XPath function fn:replace from XSLT

查看:26
本文介绍了使用 XPath 函数 fn:replace from XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XSLT 将简单的 XML 模式转换为 HTML,并计划使用 fn:replace 将返回 (\n) 替换为 <p>;.但是,我不知道如何正确使用此功能.

I'm trying to use XSLT to convert a simple XML schema to HTML and planned to use fn:replace to replace returns (\n) with <p>;. However, I can't figure out how to use this function properly.

我正在使用的 XSLT 的简化版本读取:

A simplified version of the XSLT I'm using reads:

<?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>

这个 XLST 的输入是例如:

And the input to this XLST is e.g.:

<?xml version="1.0"?>

<root>
  <value><![CDATA[
    Hello
    world!
  ]]></value>
</root>

转换在 fn:replace 上失败并出现 NoSuchMethodException.如果我将替换语句更改为

The conversion fails on fn:replace with an NoSuchMethodException. If I change the replace statement to

<xsl:value-of select="fn:replace('somestring', '\n', '<p>')" />

我收到一个 IllegalArgumentException.我如何使用 fn:replace 来实现我想要的?

I get an IllegalArgumentException. How do I use fn:replace to achieve what I want?

我正在使用 Butterfly XML 编辑器来测试 XSLT.

I'm using Butterfly XML Editor to test the XSLT.

推荐答案

XPath 2.0 具有替换功能,您可以将其与任何 XSLT 2.0 处理器(如 Saxon 9 或 AltovaXML 工具或格式塔)一起使用.您似乎尝试将该函数与 XSLT 1.0 处理器一起使用,但这是行不通的.如果您仅限于使用 XSLT 1.0 处理器,您将需要使用命名递归模板或借助扩展来实现替换.

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.

但是请注意,即使使用 XSLT 2.0,您尝试使用 replace 似乎是错误的,因为您将生成带有 'p' 标记标记的文本节点,而我假设您想在结果中创建一个 'p' 元素节点.因此,即使使用分析字符串而不是替换的 XSLT 2.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.

这篇关于使用 XPath 函数 fn:replace from XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆