[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: normalize-space problems with child elements

Subject: Re: normalize-space problems with child elements
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 29 Mar 2005 13:22:24 -0500
xsl normalize space
At 2005-03-29 12:57 -0500, I wrote:
At 2005-03-29 12:39 -0500, Jelks Cabaniss wrote:
But that does weird stuff to the `<code>` child elements -- it
removes the space before and after the `<code>foo</code>` so that the word
"foo" adjoins the surrounding text in the description output.

That's not exactly "weird", it is doing exactly what you asked for (but it isn't what you want).


How about preserving the existence of leading and trailing white-space by wrapping the node before normalization and unwrapping it after?

I realized that was sufficient to your task but not complete if you wanted to trim leading and trailing white-space of the entire element, as I do in my XSL training material. Below I use all of the template rules from my development environment.


I've edited your description with leading and trailing space, then illustrated the trimming by adding bangs before and after the description.

I hope this helps.

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

t:\ftemp>type jelks.xml
<?xml version="1.0" encoding="iso-8859-1"?>
    <description>
    This decribes how <code>FOO</code> is
    supposed to work.  Now "<code>bar</code>" is a
    different story altogether.
    </description>

t:\ftemp>type jelks.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="description">
  <xsl:text/>!<xsl:apply-templates />!<xsl:text/>
</xsl:template>

<xsl:template match="code">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="text()[preceding-sibling::node() and
                            following-sibling::node()]">
  <xsl:variable name="ns" select="normalize-space(concat('x',.,'x'))"/>
  <xsl:value-of select="substring( $ns, 2, string-length($ns) - 2 )" />
</xsl:template>

<xsl:template match="text()[preceding-sibling::node() and
                            not( following-sibling::node() )]">
  <xsl:variable name="ns" select="normalize-space(concat('x',.))"/>
  <xsl:value-of select="substring( $ns, 2, string-length($ns) - 1 )" />
</xsl:template>

<xsl:template match="text()[not( preceding-sibling::node() ) and
                            following-sibling::node()]">
  <xsl:variable name="ns" select="normalize-space(concat(.,'x'))"/>
  <xsl:value-of select="substring( $ns, 1, string-length($ns) - 1 )" />
</xsl:template>

<xsl:template match="text()[not( preceding-sibling::node() ) and
                            not( following-sibling::node() )]">
  <xsl:value-of select="normalize-space(.)"/>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>saxon jelks.xml jelks.xsl
!This decribes how FOO is supposed to work. Now "bar" is a different story altogether.!
t:\ftemp>


--
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.