RE: [xsl] removing trailing space for all the elements

Subject: RE: [xsl] removing trailing space for all the elements
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 11 Dec 2008 10:32:47 -0000
>  I need to remove trailing space for all the elements. Any 
> help appreciated.
> 
>  Input:
> 
>  <?xml version="1.0"?>
>  <Toc>
>  <a> Say you want display this <emph type="b"> data</emph> in 
> the form of a  sequentially numbered booklist.</a>  </Toc>
> 
>  Expected Output:
> 
>  <?xml version="1.0"?>
> <Toc>
> <a>Say you want display this <emph type="b">data</emph> in 
> the form of a sequentially numbered booklist.</a> </Toc>

You've actually removed two leading spacs there (before "Say" and "data",
and you have left one leading/trailing space alone (in the text node that
follows the </a> end tag. I assume what you actually want to do is not to
remove trailing spaces, but to apply normalize-space to each text node.

The easiest way to achieve that is

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

and then make sure you are outputting all text using <xsl:apply-templates/>
rather than <xsl:value-of/>
> 
> 
> Tried Coding:
> 
> <xsl:template match="*|@*">
> <xsl:copy>
> <xsl:apply-templates select="normalize-space(*)"/> 
> </xsl:copy> </xsl:template>
> 
> For the above style sheet I'm getting error "apply-templates 
> expected node()" and I'm using saxon version 8.

I hope you have some understanding of what you've done wrong there. If not,
that suggests you really haven't yet understood what apply-templates does,
and rather than give you a terse explanation here, I'd suggest you curl up
with a good book for a couple of hours...

Michael Kay
http://www.saxonica.com/

Current Thread