XSL输出一开始格式很好,但随后会折叠成一行


XSL output starts well-formatted, but then collapses into a single line

我正在使用PHP的XSLTProcessor和DOMDocument类来转换一些XML。

处理器的输出一开始格式很好(换行和缩进),但突然开始将输出集中在一行上。

    <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>

输出看起来像:

    <?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>

为什么在我开始使用article元素后,处理器似乎就不再关心格式化了?

在保存之前,我在PHP DOMDocument上设置了formatOutput和preserveWhiteSpace,这就是为什么我得到了一些格式化。

我会理解它,因为您的XSLT包括缩进,例如

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

然而,事实是,当只查看元素内容时,序列化和空白变得无关紧要。这里的这个问题详细介绍了如何在需要的情况下用PHP漂亮地打印XML。