Re: [xsl] <xsl:strip-space> does NOT work when comment lines are involved (directly before).

Subject: Re: [xsl] <xsl:strip-space> does NOT work when comment lines are involved (directly before).
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Tue, 8 Dec 2009 11:11:12 +0100
Trying to learn (!), I've come up with the one shown below.
@Ben: no guarantee!
@Others: I'd be grateful for any comments.


<?xml version="1.0"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">

  <xsl:output indent="yes"/>

  <xsl:template match="@* | element() | comment() ">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*[normalize-space( concat(.,@*) )='']"/>

  <xsl:template match="text()">
    <xsl:choose>
      <xsl:when test="preceding-sibling::*[normalize-space( concat(.,@*)
)='']">
        <xsl:copy/>
      </xsl:when>
      <xsl:when test=".[normalize-space(.) !='']">
        <xsl:copy/>
      </xsl:when>
      <xsl:otherwise>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

-W



On Tue, Dec 8, 2009 at 10:12 AM, Ben Stover <bxstover@xxxxxxxxxxx> wrote:
> In my previous posting I asked for a way to prevent blank lines.
>
> Some users recommend to insert
>
>   <xsl:strip-space elements="*"/>
>
> on top of the XSLT script. That work fine as long as there are no comments
involved (directly before).
> However when I have an input.xml file like
>
> <ns1:myelem>
> <ns2:aaa>123</ns2:aaa>
> <!-- some comment -->
> <ns2:bbb></ns2:bbb>
> <ns2:ccc>456</ns2:ccc>
> </ns1:myelem>
>
> then the resulting output.xml would be:
>
> <ns1:myelem>
> <ns2:aaa>123</ns2:aaa>
> <!-- some comment --><ns2:ccc>456</ns2:ccc>
> </ns1:myelem>
>
> Mind the appended XML node DIRECTLY AFTER the comment on the same line!
> How can I let XSLT insert/keep the CR/eol char after the comment line
> AND the keep indent (for node <ns2:ccc>?
>
> Furthermore with
>
>   <xsl:strip-space elements="*"/>
>
> ALL existing blank lines are removed. But I want to remove only these blank
lines which are created
> when empty elements are removed (with the templates of the remaining
script).
>
> How can I achieve this?
>
> Ben
>
> The current xslt script would be:
>
> <?xml version="1.0"?>
>  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
>
>   <xsl:strip-space elements="*"/>
>
>   <xsl:output indent="yes"/>
>
>   <xsl:template match="@* | node()">
>     <xsl:copy>
>       <xsl:apply-templates select="@* | node()"/>
>     </xsl:copy>
>   </xsl:template>
>
>   <xsl:template match="*[normalize-space( concat(.,@*) )='']"/>
>
> </xsl:stylesheet>

Current Thread