XSLT: How to remove the self-closed elment

Posted by Daoming Yang on Stack Overflow See other posts from Stack Overflow or by Daoming Yang
Published on 2010-03-16T12:38:38Z Indexed on 2010/03/16 12:56 UTC
Read the original article Hit count: 267

Filed under:
|

I have a large xml file which contents a lot of self-closed tags. How could remove all them by using XSLT.

eg.

<?xml version="1.0" encoding="utf-8" ?>
<Persons>
  <Person>
    <Name>user1</Name>
    <Tel />
    <Mobile>123</Mobile>
  </Person>
  <Person>
    <Name>user2</Name>
    <Tel>456</Tel>
    <Mobile />
  </Person>
  <Person>
    <Name />
    <Tel>123</Tel>
    <Mobile />
  </Person>
  <Person>
    <Name>user4</Name>
    <Tel />
    <Mobile />
  </Person>
</Persons>

I'm expecting the result:

<?xml version="1.0" encoding="utf-8" ?>
<Persons>
  <Person>
    <Name>user1</Name>
    <Mobile>123</Mobile>
  </Person>
  <Person>
    <Name>user2</Name>
    <Tel>456</Tel>
    </Person>
  <Person>
    <Tel>123</Tel>
  </Person>
  <Person>
    <Name>user4</Name>
  </Person>
</Persons>

Note: there are thousands of different elements, how can I programmatically remove all the self-closed tags. Another question is how to remove the empty element such as <name></name> as well.

Can anyone help me on this? Many thanks.

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xsl