M
M
Muhammad2016-08-31 21:12:03
HTML
Muhammad, 2016-08-31 21:12:03

How to remove spaces in XML using XSLT?

There is this XML:

<?xml version="1.0" encoding="UTF-8"?>
<content>
    <p>Lorem ipsum dolor sit amet,</p>

    <p>Quibusdam dolore iure deleniti quasi accusantium non nostrum facere neque ducimus
    mollitia et ex suscipit, placeat, eius maiores ratione cumque. <b>Provident</b> doloribus
    natus perspiciatis quas id sequi neque, molestiae cupiditate facere quod. Dolore voluptate
    consectetur:</p>

    <p>aperiam nihil.</p>

    <dialog>
        aperiam nihil.
        aperiam <b>123</b> nihil.
        aperiam nihil.
        <b>123</b>
    </dialog>

    <img src="images/my-image.jpg"/>
</content>

You need to take the contents of the dialogue tag (text mixed with tags), remove the spaces and wrap the whole thing in a div. In this case, you need to keep the tags inside. Wrote this code:
<xsl:template match="dialog">
    <p class="dialog">
        <xsl:copy-of select="node()"/>
    </p>
</xsl:template>

Tags are preserved, but spaces are preserved along with them. Is it possible in XSLT to remove spaces in XML code?
XSLT 1.0. I don't want to do it programmatically.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Philipp, 2016-08-31
@zoonman

xsl:strip-space

T
theg4sh, 2016-09-21
@theg4sh

Muhammad I correctly understood - "deleting extra spaces"?
beautiful and "little code" will not work - the replace function with regexp support is available only for version >=2.0.
For version 1.0, the only suitable solution for the task will be:

<xsl:stylesheet version="1.1"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="p">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="dialog">
        <p class="dialog">
            <xsl:for-each select="./*|./text()">
                <xsl:choose>
                    <xsl:when test="self::text()">
                        <xsl:value-of select="normalize-space(.)" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text> </xsl:text>
                        <xsl:copy>
                            <xsl:copy-of select="./text()"/>
                        </xsl:copy>
                        <xsl:text> </xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </p>
    </xsl:template>

</xsl:stylesheet>

The result will be:
<?xml version="1.0"?>

    <p>Lorem ipsum dolor sit amet,</p>

    <p>Quibusdam dolore iure deleniti quasi accusantium non nostrum facere neque ducimus
    mollitia et ex suscipit, placeat, eius maiores ratione cumque. <b>Provident</b> doloribus
    natus perspiciatis quas id sequi neque, molestiae cupiditate facere quod. Dolore voluptate
    consectetur:</p>

    <p>aperiam nihil.</p>

    <p class="dialog">aperiam nihil. aperiam <b>123</b> nihil. aperiam nihil. <b>123</b> </p>

Of the minuses - there will be a restriction on the nested tree of elements, although I think you can finish the example before using the template in case otherwise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question