2013-11-27 14 views
-1

我使用PHP的XSLTProcessor和DOMDocument類來轉換某些XML。XSL輸出開始格式良好,但隨後摺疊成一行

處理器的輸出開始格式良好(換行符和縮進),但突然它開始在一行中輸出輸出。

<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,這就是爲什麼我得到一些格式。

回答

0

我想知道它的原因,因爲你的XSLT包括縮進例如。

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

    <content> 
     <article> 

然而,事情的事實是,在剛剛元素含量看時像你這樣,序列化和空白變得無關緊要。 This question here詳細介紹瞭如何在PHP中打印XML,如果需要的話。

+0

我不確定你的意思...你能提供一個如何解決的例子嗎?我正在使用XML的PHP​​漂亮打印選項 - 但它看起來像是從XSLT – HorusKol

+0

hmmm開始 - 當我去掉所有的'和''I仍然在'

+0

中得到相同的錯誤格式爲什麼你需要格式化? XML的漂亮打印是爲了讓其他人更容易閱讀,它對文檔本身的影響很小。 – 2013-11-27 04:33:40