I'm using PHP's XSLTProcessor and DOMDocument classes to transform some XML.
The output from the processor starts well-formatted (newlines and indents), but suddenly it starts lumping the output on a single line.
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<!-- We are outputting and XML file for consumption by the electronic platforms
we can disable indenting at this stage as the build controller is formatting the output when saving the XML to file -->
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="UTF-8" />
<!-- this parameter is passed to the XSLT processor from the build controller -->
<xsl:param name="id" />
<!-- we can't use a parameter in a template's match attribute, so we have to do this as a work around
we are applying templates to the element whose ID matches the parameter from the build script -->
<xsl:template match="/">
<xsl:apply-templates select="//*[@id = $id]" mode="document" />
</xsl:template>
<!-- the document structure -->
<xsl:template match="*" mode="document">
<document>
<xsl:attribute name="class">
<xsl:value-of select="name()" />
</xsl:attribute>
<content>
<article>
<xsl:attribute name="class">
<xsl:value-of select="name()" />
</xsl:attribute>
<!-- this is the anchor used for links to return to the top of the page/document -->
<xsl:attribute name="id">
<xsl:text>top</xsl:text>
</xsl:attribute>
<!-- the reference of the product to be displayed when printing -->
<footer id="reference">
<xsl:text>Product reference</xsl:text>
</footer>
<header>
<xsl:attribute name="id">
<xsl:value-of select="$id" />
</xsl:attribute>
<h1><xsl:apply-templates select="title" /></h1>
</header>
<!-- apply any templates that match the elements within the source document -->
<xsl:apply-templates />
</article>
</content>
</document>
</xsl:template>
</xsl:stylesheet>
The output looks like:
<?xml version="1.0"?>
<document class="guide">
<content>
<article class="guide" id="top"><footer id="reference">Product reference</footer><header id="guide.to.book"><h1>Using the book</h1></header></article>
</content>
</document>
Why does the processor just seem to stop caring about formatting once I've started in the article element?
I am setting formatOutput and preserveWhiteSpace on the PHP DOMDocument before saving, which is why I'm getting some formatting.