3

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

Community
  • 1
  • 1
C D
  • 57
  • 2
  • 8

1 Answers1

1

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>

...
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Works perfect and answers my question perfectly! Thanks a lot! – C D Sep 28 '16 at 07:49
  • Your code works just as it should. But now I see that my question wasn't correct. The output is: "1/4\"";"1/4\"";""; if "currentValue" is empty. No problem there. But as soon as I open the CSV in Excel it Displays the output in one cell like: 1/4\";1/4\"" it should put it in different cells. I thought that this Problem would be solved by replacing " by \". But Excel doesn't seem to understand this. – C D Sep 28 '16 at 09:07
  • Try changing the `replaceString` param to `""` - see: https://en.wikipedia.org/wiki/Comma-separated_values#Standardization – michael.hor257k Sep 28 '16 at 09:50
  • You sir, are my hero! Thank you very much! – C D Sep 28 '16 at 10:07