We help IT Professionals succeed at work.

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.

TIA
Comment
Watch Question

Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
<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)

Author

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.

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

Open in new window

Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
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>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
   
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
</xsl:stylesheet>


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

Author

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.

Kurt
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
cheers