[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Remove adjacent whitespace when removing element


Subject: Re: [xsl] Remove adjacent whitespace when removing element
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 14 Nov 2013 22:23:00 -0700

At 2013-11-14 19:48 -0700, Karl Stubsjoen wrote:
I'm writing a little simple XSLT that translates one Xml to another
and I am cherry picking items from the first Xml for the new Xml and
doing an assortment of other things as well.  All is fine and dandy,
but I'd like to trap the whitespace along with the elements that I am
removing, instead I am getting big gaps in my resulting Xml.  I
reallize that I can turn off indent, but what would be the way to
remove the related whitespace of an element that has been removed?  So
given:

<data>
<a>this is a</a>
<b>this is b</b>
<c>this is c</c>
</data>

If I trap b <xsl:template match="b"/> and do nothing with it my result
looks like:

<data>
<a>this is a</a>

<c>this is c</c>
</data>

Thanks.

Something like the following (untested)?


<xsl:template match="text()[not(normalize-space())]
                           [preceding-sibling::node()[self::b]"/>

Or just this combination (also untested) though it will massage your white-space and may have a negative impact on mixed content that looks like element content:

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

I hope this helps.

. . . . . . . Ken


-- Public XSLT, XSL-FO, UBL & code list classes: Melbourne, AU May 2014 | Contact us for world-wide XML consulting and instructor-led training | Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm | Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ | G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx | Google+ profile: https://plus.google.com/116832879756988317389/about | Legal business disclaimers: http://www.CraneSoftwrights.com/legal |


Current Thread
Keywords