Re: [xsl] normalize-space and the identity template

Subject: Re: [xsl] normalize-space and the identity template
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 04 May 2010 21:08:11 -0700
At 2010-05-04 22:54 -0500, Keith Gilbert wrote:
I have the following xml:

<Company>
      <Title>Federal Civil Trial </Title>
      <Author>by John Doe </Author>
      <Body>Blah blah blah blah blah. </Body>
</Company>

I need to remove the extra spaces that sometimes occur before the end-tags.

I've been messing around with the identity template, and can get that to copy my XML intact.

The following trims the spaces before the end-tags nicely.

<xsl:template match="Company/Title">
      <xsl:copy><xsl:value-of select="normalize-space()"/></xsl:copy>
</xsl:template>
<xsl:template match="Company/Author">
      <xsl:copy><xsl:value-of select="normalize-space()"/></xsl:copy>
</xsl:template>

But what I'm wondering is if there's a way to use the identity template to copy the whole tree, but still somehow use normalize-space to remove the spaces. This would enable me to not have to match each element of the tree individually (there are a lot of possible elements).

I can't quite get me head around this one. Any pointers in the right direction are appreciated.

As a pointer (but I don't have time to mock something up for you), remember you can match on the text nodes themselves:


  <xsl:template match="Company/*//text()">
    <!--remove the last spaces of the text node when there are no others
        before the child of Company-->
  </xsl:template>

It's the testing if it is the last node that I have to think about but can't take the time, because the algorithm has to accommodate possibly very deep mixed content. Maybe something along the lines of when you are at a matched descendant text node:

   <xsl:if test="generate-id(following::text[1])=
                 generate-id(ancestor::*[parent::Company]/following::text[1])">
     <!--then this is the very last text node of all text nodes
         descending from the child of Company-->
   </xsl:if>

(untested)

BTW, I'm stuck having to use XSL 1.0 for this project.

Not a problem with my approach above.


I hope this is helpful as a direction ... sorry I can't provide more right now or test my initial thoughts above.

. . . . . . . . . . . Ken

--
Principles of XSLT for XQuery Writers: San Francisco,CA 2010-05-03
XSLT/XQuery/UBL/Code List training: Trondheim,Norway 2010-06-02/11
Vote for your XML training:   http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread