Skip to content
Commit 0602c535 authored by Nick Wellnhofer's avatar Nick Wellnhofer Committed by Daniel Veillard
Browse files

Rewrite EXSLT string:replace to be conformant

For https://bugzilla.gnome.org/show_bug.cgi?id=569703

The libexslt implementation of str:replace fails to conform to its
specification on several counts:

a) the current version returns a string; it's supposed to
   return a nodeset.

b) the current version treats the replacements as strings;
   it's supposed to treat them as nodes.

c) the current version can modify replacement text; it's
   supposed to only modify text from the original string.

d) the current version ignores the requirement to perform
   substitutions in descending order of search string length.

Steps to reproduce:
a) the returning of a string rather than a nodeset can be seen
   by simply inspecting the code.

b) the code explicity converts replacement nodes to strings;
   this can be seen by inspection.
d) the failure to perform substitutions in descending order
   of search string length can be seen in the lack of any
   sorting in the source code.

c) the problem of modifying text not belonging to the original
   string can be seen in the following stylesheet, which can
   be simply applied to itself to produce output.

<xsl:stylesheet version="1.0"
    extension-element-prefixes="str exsl"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    xmlns:str="http://exslt.org/strings"
    >
<xsl:variable name="Text">
    Price is $1.10
</xsl:variable>

<xsl:template match="/">
    <xsl:apply-templates select="exsl:node-set($Text)/text()"/>
</xsl:template>
<xsl:template match="text()">
   <xsl:variable name="Replace">
        <FromXml>
            <from>$</from>
            <from>\</from>
        </FromXml>
        <ToTex>
            <to>\$</to>
            <to>$\backslash$</to>
        </ToTex>
    </xsl:variable>
    <xsl:value-of

select="str:replace(.,exsl:node-set($Replace)/FromXml/from,exsl:node-set($Replace)/ToTex/to)"/>
</xsl:template>
</xsl:stylesheet>

Actual results:
The output is:

<?xml version="1.0"?>

    Price is $\backslash$$1.10

Expected results:
The output should be:
<?xml version="1.0"?>

    Price is \$1.10

Does this happen every time?
yes.

Other information:
str:replace specification is at:

http://www.exslt.org/str/functions/replace/str.replace.html
parent 39b0c27a
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment