0

I'm looking to replace multiple consecutive new line characters with a single new line character in the result of an XSLT.

Within an xml node I could have the following:

<description>
    A description that goes over multiple lines, because:

        - someone inputted the data in another system
        - and there are gaps between lines

    Another gap above this
</description>

I'd like the text out of this node to appear as so:

A description that goes over multiple lines, because:
    - someone inputted the data in another system
    - and there are gaps between lines   
Another gap above this

Is there a way to do this with an XSLT? Using XSLT 1.0 (libxslt)

2
  • Are you using XSLT 1.0 or 2.0? Commented Jul 4, 2016 at 5:52
  • I'm using libxml 1.1.28, which from what I can tell uses XSLT 1.0 (xmlsoft.org/XSLT) Commented Jul 4, 2016 at 5:57

1 Answer 1

2

How about:

<xsl:template match="description">
    <xsl:call-template name="normalize-returns">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="normalize-returns">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&#10;&#10;')">
            <!-- recursive call -->
            <xsl:call-template name="normalize-returns">
                <xsl:with-param name="text">
                    <xsl:value-of select="substring-before($text, '&#10;&#10;')"/>
                    <xsl:text>&#10;</xsl:text>
                    <xsl:value-of select="substring-after($text, '&#10;&#10;')"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
Sign up to request clarification or add additional context in comments.

1 Comment

That's fantastic! I had to make a few changes to fit into my code, but essentially worked well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.