Using XPath fn function: replace XSLT

I am trying to use XSLT to convert a simple XML schema to HTML and planned to use fn:replace to replace return ( \n ) with <p>; . However, I cannot figure out how to use this function correctly.

The simplified version of XSLT that I use 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> 

And the input to this XLST is, for example:

 <?xml version="1.0"?> <root> <value><![CDATA[ Hello world! ]]></value> </root> 

The conversion fails with an fn:replace error with a NoSuchMethodException. If I replaced the replace statement with

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

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

I am using Butterfly XML Editor to test XSLT.

+7
xml xpath xslt
source share
2 answers

XPath 2.0 has a replacement function that you can use with any XSLT 2.0 processor, such as the Saxon 9 or AltovaXML or Gestalt tools. It seems you are trying to use this feature with an XSLT 1.0 processor that will not work. If you are limited to the XSLT 1.0 processor, you will need to implement the replacement using a recursive template or using an extension.

However, note that even with XSLT 2.0, your attempt to use a replacement seems to be wrong, because you will create node text with the p tag tag, while I assume that you want to create the p node element in the result. Thus, even with XSLT 2.0 using an analytic line instead of a replacement, you are more likely to get the desired result.

+7
source share

Inclining the version attribute <xsl:stylesheet> to 2.0 and using

<xsl:value-of select="replace(description, '\n', '<p/>')" disable-output-escaping="yes" />

I was able to complete the replacement.

0
source share

All Articles