Use PEAR's XML_Util class:
- <?php
- $xmlData =<<< END
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head></head>
- <body>
- <div>Content here.</div>
- <p></p>
- <div>Content here.</div>
- </body>
- </html>
- END;
- // include class
- include "XML/Util.php";
- // collapse empty tags and print
- echo XML_Util::collapseEmptyTags($xmlData);
- ?>
Comments
For elements that have no content, such as <name></name>, the XML specification suggests the use of empty-element tags, such as <name />. Available from http://pear.php.net/package/XML_Util, PEAR's XML_Util class provides an automated way to accomplish this change via its collapseEmptyTags() method. One of the most common uses for this method is illustrated in the previous listing: replace HTML's <p></p> sequence with the "better" <p /> sequence.