xslt1.0 replace is not working

后端 未结 1 741
悲&欢浪女
悲&欢浪女 2021-01-25 09:52

I have URLs with some special characters and i would like to replace them and I am using xslt 1.0 so I am writing the code as below.



        
相关标签:
1条回答
  • 2021-01-25 10:17

    The provided "replace" template is working.

    To confirm this use the following transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
        <xsl:template match="/*">
            <xsl:variable name="link">
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="@FileRef"/>
                    <xsl:with-param name="replace">'</xsl:with-param>
                    <xsl:with-param name="by" select="'%27'"/>
                </xsl:call-template>
            </xsl:variable>
    
            "<xsl:value-of select="$link"/>"
        </xsl:template>
    
        <xsl:template name="string-replace-all">
            <xsl:param name="text"/>
            <xsl:param name="replace"/>
            <xsl:param name="by"/>
            <xsl:choose>
                <xsl:when test="contains($text,$replace)">
                    <xsl:value-of select="substring-before($text,$replace)"/>
                    <xsl:value-of select="$by"/>
                    <xsl:call-template name="string-replace-all">
                        <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                        <xsl:with-param name="replace" select="$replace"/>
                        <xsl:with-param name="by" select="$by"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$text"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    when the transformation is applied on this XML document:

    <t FileRef="Abc'Xyz'Tuv"/>
    

    the wanted, correct result is produced:

    "Abc%27Xyz%27Tuv"
    

    The problem may be in the way you specify the replace parameter (uneven number of apostrophes in the expression):

    Instead of:

    <xsl:with-param name="replace" select="'&#39;'"/>
    

    Use:

    <xsl:with-param name="replace">'</xsl:with-param>
    
    0 讨论(0)
提交回复
热议问题