Search code examples
xmlxslt-2.0

Need to remove empty space <p> Tag


I want to remove the empty space <p> element using XSL:

XML I'm having:

    <Body>
      <p> </p>
      <h1>AAA</h1>
      <p>aaa</p>
    </Body>

XSL I Used:

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

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

   <xsl:template match="h1"/>

   <xsl:template match="p[not(normalize-space())]"/>

Output I'm getting like:

    <Body>
      <h1>AAA</h1>
      <p> </p>
      <p>aaa</p>
    </Body>

Expected output be like:

    <Body>
      <h1>AAA</h1>
      <p>aaa</p>
    </Body>

I Need to remove the empty space para tags. Please suggest code. Thanks in advance


Solution

  • You can try something like this

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="@*|node()">
        <xsl:if test="normalize-space(.) != ''">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>