public function asPrettyXML($filepath = null)
    {
        /**
         * Dump and reload this node's XML with LIBXML_NOBLANKS.
         *
         * Also import it as a DOMDocument because some older of XSLTProcessor rejected
         * SimpleXMLElement as a source.
         */
        $xml = dom_import_simplexml(
            new SimpleXMLElement(
                $this->asXML()
            )
        );

        $xsl = new DOMDocument;
        $xsl->loadXML(
            '<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="xml" indent="yes" />

	<xsl:template match="text()">
		<!-- remove everything that contains only whitespace, with at least one LF -->
		<xsl:if test="not(normalize-space(.) = \'\' and contains(., \'&#10;\'))">
			<xsl:value-of select="."/>
		</xsl:if>
	</xsl:template>

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

</xsl:stylesheet>'
        );

        $xslt = new XSLTProcessor;
        $xslt->importStylesheet($xsl);

        $result = trim($xslt->transformToXML($xml));

        if (isset($filepath)) {
            return (bool)file_put_contents($filepath, $result);
        }

        return $result;
    }