Link to home
Start Free TrialLog in
Avatar of Boopathy S
Boopathy SFlag for India

asked on

Need to remove the space element values using XSL

I have two types of XML elements, one type is

First XML:

<Body>
<h1>aaa</h1>
<h1>bbb</h1>
<h1>ccc</h1>
</Body>

Second XML:

<Body>
<h1> </h1>
<h1>aaa</h1>
<h1>bbb</h1>
</Body>

XSL I'm using as

<xsl:strip-space elements="h1"/>

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

   <xsl:template match="Body">
      <xsl:copy>
         <h1><xsl:value-of select="h1"/></h1>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="h1"/>

Output I'm getting correctly for first XML as like:

<Body>
<h1>aaa bbb ccc</h1>
</Body>

But even I used strip-space element as h1 for the second xml, I'm getting like below:

<Body>
<h1> aaa bbb</h1>
</Body>

Expected output be like

<Body>
<h1>aaa bbb</h1>
</Body>

Need to remove the space element of h1. But due to that tag, its combining even the space. I used priority and used the seperate h1[normalize-space()] tag also in the template. But its not working. There is having any way to avoid this. Please suggest. I'm using XSLT 2.0 and saxon-PE 9.6.0.7. Thanks in advance
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

strip-space strips the space, not the element

let the apply-templates mechanism do the work and use predicate conditions

<xsl:copy>
         <xsl:apply-templates select="h1"/>
         <xsl:apply-templates select="@*|node()[not(self::h1)]"/>
      </xsl:copy>

Open in new window


instead of

<xsl:copy>
         <h1><xsl:value-of select="h1"/></h1>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the generic template for node() will pick up the h1 that does have value
Avatar of Boopathy S

ASKER

Hi Geert. Thanks. Its working fine for the below tag

<h1><xsl:value-of select="h1[normalize-space()]" separator=" "/></h1>
Thanks