0

Please, help me with the following

I guess it's normalize space function, but not sure, how exactly to use it.

1) How to find all Empty elements and remove spaces from it

for example need to transform

<li>     </li>

to

<li></li>

2) How to make more exact transformation? Find all empty LI in UL with class "list"

<ul class="list">
    <li>    </li>
    <li>Hello world!</li>
</ul>

and transform it to

<ul class="list">
    <li></li>
    <li>Hello world!</li>
</ul>

Thank you very much

  • There is question concerning trimming in xslt 1.0. Take a look here : http://stackoverflow.com/questions/12458583/is-there-any-operation-such-as-trim-in-xslt – parakmiakos Sep 18 '14 at 09:32
  • @parakmiakos In my opinion, the question you link to is about something else (i.e. trimming as implemented in e.g. Java). This question is about XML elements containing only whitespace. – Mathias Müller Sep 18 '14 at 10:34

1 Answers1

1

First of all, saying that your input XML contains "empty elements with whitespaces in them" is very misleading. Elements that have whitespace characters as their content are not empty at all. Instead, they contain a text node as a child, that in turn contains only whitespace characters.

You can remove the whitespace content of otherwise empty elements with the XSLT element strip-space.

This only affects whitespace-only text nodes of elements, for example

<li>    </li>

On the other hand, strip-space will not remove the whitespace in text nodes like

<li>dhd   ddj     d</li>

To also normalize the whitespace of text nodes that contain characters other than whitespace, use the normalize-space() function:

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

normalize-space() removes trailing whitespace and replaces any sequence of whitespace characters with a single whitespace.

Stylesheet (XSLT 1.0)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:strip-space elements="*"/>

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

</xsl:transform>

XML Input

<ul class="list">
    <li>    </li>
    <li>Hello world!</li>
</ul>

XML Output

<ul class="list">
   <li/>
   <li>Hello world!</li>
</ul>

Another code example, that illustrates the use of normalize-space():

XML Input

<ul class="list">
    <li>    </li>
    <li>Hello world!    </li>
</ul>

Stylesheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="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:transform>

XML Output

<ul class="list">
   <li/>
   <li>Hello world!</li>
</ul>
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • Thank you very much (really) for such detailed helpful answer! – user3497585 Sep 18 '14 at 11:25
  • 1
    @user3497585 You are welcome. Please consider accepting this answer, if it solved your problem (click the tick on the left so that it appears in green). Also do this for previous questions you have asked, for example [this one](http://stackoverflow.com/questions/23340252/transform-blocks-to-select-box). – Mathias Müller Sep 18 '14 at 11:30