Removing empty tags from XML via XSLT

Viewed 41334

I had an xml of the following pattern

<?xml version="1.0" encoding="UTF-8"?>
    <Person>
      <FirstName>Ahmed</FirstName>
      <MiddleName/>
      <LastName>Aboulnaga</LastName>
      <CompanyInfo>
        <CompanyName>IPN Web</CompanyName>
        <Title/>
    <Role></Role>
        <Department>
    </Department>
      </CompanyInfo>
    </Person>

I used the following xslt (got from forums) in my attempt to remove empty tags

 <xsl:template match="@*|node()">
<xsl:if test=". != '' or ./@* != ''">
  <xsl:copy>
  <xsl:copy-of select = "@*"/>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:if>

The xslt used is successful in removing tags like

<Title/>
    <Role></Role>

...but fails when empty tags are on two lines, eg:

<Department>
    </Department>

Is there any fix for this?

6 Answers
Related