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

时间:2013-11-27 02:03:31

标签: php xslt

我正在使用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>

为什么一旦我开始使用文章元素,处理器似乎就不再关心格式了?

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

1 个答案:

答案 0 :(得分:0)

我想了解它,因为你的XSLT包括缩进,例如。

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

    <content>
        <article>

然而,问题的实际情况是,在查看您所拥有的元素内容时,序列化和空白变得无关紧要。 This question here详细介绍了如果需要在PHP中打印XML的方法。