-1

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.

HorusKol
  • 8,375
  • 10
  • 51
  • 92

1 Answers1

0

I'd fathom its because your XSLT includes indenting eg.

<document>
    <xsl:attribute name="class">
        <xsl:value-of select="name()" />
    </xsl:attribute>

    <content>
        <article>

However, the fact of the matter is that when looking at just element content like you have, the serialisation and whitespace become irrelevant. This question here has details on how to pretty print XML in PHP if it is desired.

Community
  • 1
  • 1
  • I'm not sure what you mean... can you provide an example on how to fix? And I am using the PHP pretty print options for XML - but it looks like the problem is starting at the XSLT – HorusKol Nov 27 '13 at 04:12
  • hmmm - when I strip out all the `` and `` I still get the same bad formatting within the `
    `
    – HorusKol Nov 27 '13 at 04:24
  • Why do you need formatting? Pretty printting of XML is to make it easier to read for other humans, it does very little to the document itself. –  Nov 27 '13 at 04:33
  • To make it easier to read and work with once it's in the file - I know that I can read it easily enough using Chrome, and it might be enough. I just want to know why the processor gives up at `
    `
    – HorusKol Nov 27 '13 at 05:01