Russell'sTeapot
Russell'sTeapot

Reputation: 383

XSLT transformation to remove space within element

I am a bit new to XSLT stuff. The problem is that I need to get rid of whitespaces within certain elements in my input XML. For example

<element id="12">


</element>

should be transformed into

<element id="12"></element>

and also

<element id="12">

something

</element>

into

<element id="12">something</element>

and the rest of the xml should remain the same. Is this kind of transformation possible with xsl?

Upvotes: 1

Views: 4773

Answers (2)

hr_117
hr_117

Reputation: 9627

What you like to do is an Identity transform.

Try something like this:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space()"/>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 4

R Kaja Mohideen
R Kaja Mohideen

Reputation: 917

Hope, this will help you.

<xsl:template match="text()">
    <xsl:value-of select="fn:normalize-space()"/> <!-- fn is the prefix bound to xpath functions -->
</xsl:template>

Upvotes: 1

Related Questions