C D
C D

Reputation: 57

XSLT 1.0: escaping double quotes

I have a XML file following this scheme:

<translationData>
<product>
<attributeValue>
<value>1/4"</value>
<value1>1/4"</value1>
<currentValue>aaaa;bbbb</currentValue>
</attributeValue>
</product>
</translationData>

because of the semicolon in "currentValue" i need to escape the semicolon AND the double quotes in "value". I am able to escape the semicolon by placing all text in qoutes as following:

XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="utf-8" />

  <xsl:param name="delim" select="';'" />
  <xsl:param name="quote" select="'&quot;'" />
  <xsl:param name="break" select="'&#xA;'" />

  <xsl:template match="/">
    <xsl:apply-templates select="translationData/product/attributeValue" />
  </xsl:template>

  <xsl:template match="attributeValue">
    <xsl:apply-templates />
    <xsl:if test="following::*">
      <xsl:value-of select="$break" />
    </xsl:if>
  </xsl:template>


  <xsl:template match="*">
    <!-- remove normalize-space() if you want keep white-space at it is -->

    <xsl:value-of select="concat($quote, translate(.,'&quot;','\&quot;'), $quote)" />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$delim" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="text()" />
</xsl:stylesheet>

but somehow the Output is:

"1/4\";"1/4\";"aaaa;bbbb"

instead of

"1/4\"";"1/4\"";"aaaa;bbbb"

Where am I going wrong?

I am new to XML and XSLT and did not find any question handling this specific case.


XSLT code is from an answer by @Tomalak for another question. see here

Upvotes: 3

Views: 4267

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117175

The translate() function will only replace each single character with another single character.

To replace a single character " with a two-character string\" you need to use a named recursive template or - if your processor supports it - an extension function such as EXSLT str:replace().

Here's an example of using a recursive template:

...

<xsl:template match="*">
    <xsl:text>"</xsl:text>
    <xsl:call-template name="replace">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
    <xsl:text>"</xsl:text>
    <xsl:if test="position()!=last()">
        <xsl:text>;</xsl:text>
    </xsl:if>
</xsl:template>

...

<xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="searchString">"</xsl:param>
    <xsl:param name="replaceString">\"</xsl:param>
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:value-of select="substring-before($text,$searchString)"/>
            <xsl:value-of select="$replaceString"/>
           <!--  recursive call -->
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="substring-after($text,$searchString)"/>
                <xsl:with-param name="searchString" select="$searchString"/>
                <xsl:with-param name="replaceString" select="$replaceString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

...

Upvotes: 1

Related Questions