• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 912
  • Last Modified:

normalize-space for entire document?

Does anyone have an idea how to use normalize-space for an entire document? I have a mixutre of element and mixed nodes, and keeping the mixed nodes proper has been difficult.

  • 3
  • 2
1 Solution
Geert BormansInformation ArchitectCommented:
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="elements go here space seperated"/>

tends to work, in the preserve space you could put the mixed content elements, for whom space preservation is important
that ofcourse doesn't normalize the whitespace inside PCDATA streams
note that msxml and xml spy do the wrong thing with whitespace only text nodes

or you could make a multi-pass, with the second pass being an identity transform that normalize-spaces every text node
in XSLT2, you can easily add both passes inone stylesheet
(or you could do that using node-set() if your processor supports that)
kmartin7Author Commented:
Hi Geert,

For some reason, I have never been able to get that to work. I have tried it on different parsers with the same result each time. Here is a snippet. As you can see, we would like to remove all that extra space for the authors.

<para>Text and more text (
          <xref type="figure" idref="i0f3a011"></xref>) text and more text:

Open in new window

Geert BormansInformation ArchitectCommented:
try this

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:preserve-space elements="para"/>
    <xsl:template match="*">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>

I have the indent=yes in the output element, to make sure that not everything is on one line
This way you would loose spaces in front of, or right after mixed content elements, such as <emphasis>
I bet that is not acceptable
you could have a special template for all mixed content elements such as emphasis,
to copy the space back in
kmartin7Author Commented:
Yeah, it doesn't quite give us what we need. It is just a bad having all collapse. We are just going to use SS with the editor options and then run a series of find/replace to add spaces before and after certain instances.

Thanks anyway.

Geert BormansInformation ArchitectCommented:

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now