Pacerier
Pacerier

Reputation: 89623

xslt fn:replace not working as expected

i'm trying to replace the string location to tocation so basically this is my code:

<div id="{fn:replace('location','l','t')}">

but instead of giving me tocation it gives me t ?!

Live Example: go to http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 In the second textarea put in this code and hit the button Edit and Click Me:

<?xml version="2.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <div>
    <xsl:value-of select="fn:replace('location','l','t')"/>
  </div>
</xsl:template>
</xsl:stylesheet>

Upvotes: 2

Views: 1478

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

I can't repro the reported problem.

When I perform this transformation using both Saxon 9.1.05 and XQSharp:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <div id="{replace('location','l','t')}"/>
 </xsl:template>
</xsl:stylesheet>

on any XML document (not used), the result in both cases is the expected, correct one:

<div xmlns:xs="http://www.w3.org/2001/XMLSchema" id="tocation"/>

This means that you are using a non-compliant/buggy XSLT 2.0 processor.

Contact the vendor and file a bug.

In case you are using an XSLT 1.0 processor, you will need to write a recursive named template to do simple generic replace (not using RegExes). There are a number of good examples of such in the "xslt" tag that can be found in answers to similar questions.

The specific change you want to perform, however, can be done even more easily:

<div id="{translate('location','l','t')}"/>

Upvotes: 3

Related Questions