B
B
bik2017-09-30 14:03:44
XSL & XSLT
bik, 2017-09-30 14:03:44

XSL transformation -> CSV -> how to replace newline character?

Good afternoon!
There is an extension that converts data from XML to CSV.
In the fields "S_Street" and "R_Street" there are data in several lines, which of course breaks the CSV format.
Please tell me. how to replace the newline character in the "S_Street" and "R_Street" fields with a semicolon (" ,").
Thank you.
Here it is not possible to insert the code itself, since the message format breaks. Link: https://drive.google.com/file/d/0B8P7x0eHO8zNNmtuS...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Fov, 2017-10-01
@Roman-Fov

Without regex like this:

<documentRecipientAddressData>
      <street>Centroallee 1000
Einfahrt Parkhaus 7 an der "Alten Waltz" gegenüber Tryp Hotel ...</street>
      <street>Centroallee
1000
Einfahrt
Parkhaus 7 an der
"Alten Waltz" gegenüber Tryp Hotel ...</street>
      <street>Centroallee 1000 Einfahrt Parkhaus 7 an der "Alten Waltz" gegenüber Tryp Hotel ...</street>
</documentRecipientAddressData>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/documentRecipientAddressData/street">
    <xsl:variable name="street">
      <xsl:call-template name="replace-line-breaks">
        <xsl:with-param name="text">
          <xsl:call-template name="normalize-line-breaks">
            <xsl:with-param name="text" select="."/>
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="normalize-space($street)"/>
    <xsl:text>&#xa;</xsl:text>
    <xsl:variable name="street">
      <xsl:call-template name="normalize-line-breaks">
        <xsl:with-param name="text" select="."/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="replace($street, '\n', ', ')"/>
    <xsl:text>&#xa;&#xa;</xsl:text>
  </xsl:template>

  <xsl:template name="replace-line-breaks">
    <xsl:param name="text"/>

    <xsl:choose>
      <xsl:when test="not(contains($text, '&#xa;'))">
        <xsl:value-of select="$text"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="substring-before($text, '&#xa;')"/>
        <xsl:text>,&#160;</xsl:text>
        <xsl:call-template name="replace-line-breaks">
          <xsl:with-param name="text" select="substring-after($text, '&#xa;')"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="normalize-line-breaks">
    <xsl:param name="text"/>

    <xsl:choose>
      <xsl:when test="not(contains($text, '&#xa;&#xa;'))">
        <xsl:value-of select="$text"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="text">
          <xsl:value-of select="substring-before($text, '&#xa;&#xa;')"/>
          <xsl:text>&#xa;</xsl:text>
          <xsl:value-of select="substring-after($text, '&#xa;&#xa;')"/>
        </xsl:variable>
        <xsl:call-template name="normalize-line-breaks">
          <xsl:with-param name="text" select="$text"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Centroallee 1000, Einfahrt Parkhaus 7 an der "Alten Waltz" gegenüber Tryp Hotel ...
Centroallee 1000, Einfahrt Parkhaus 7 an der "Alten Waltz" gegenüber Tryp Hotel ...

Centroallee, 1000, Einfahrt, Parkhaus 7 an der, "Alten Waltz" gegenüber Tryp Hotel ...
Centroallee, 1000, Einfahrt, Parkhaus 7 an der, "Alten Waltz" gegenüber Tryp Hotel ...

Centroallee 1000 Einfahrt Parkhaus 7 an der "Alten Waltz" gegenüber Tryp Hotel ...
Centroallee 1000 Einfahrt Parkhaus 7 an der "Alten Waltz" gegenüber Tryp Hotel ...

How so
In your template, you yourself will adapt
PS: {username}, maybe there is a better way? I'd be happy to hear from those who don't care.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question