2010-03-08 87 views
8

我使用的Xalan具有以下XSL標題:如何使xsl轉換縮進輸出?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0" 
    xmlns:redirect="http://xml.apache.org/xalan/redirect" 
    extension-element-prefixes="redirect" 
    xmlns:xalan="http://xml.apache.org/xalan"> 
<xsl:output method="text" indent="yes" xalan:indent-amount="4"/> 

,輸出是不縮進。

有想法的人嗎?

+0

我在記事本++中使用了xsl工具。當我在xsl中輸入錯誤時,它無法縮進輸出。驗證您的xsl文件是否具有正確的語法。 – flobadob 2013-10-09 10:44:14

回答

17

縮進你需要使用不同的命名空間:「http://xml.apache.org/xslt」(見this issue

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0" 
xmlns:redirect="http://xml.apache.org/xalan/redirect" 
extension-element-prefixes="redirect" 
xmlns:xalan="http://xml.apache.org/xslt"> 
<xsl:output method="xml" indent="yes" xalan:indent-amount="4"/> 
+0

The xalan命名空間是在https://xalan.apache.org/xalan-j/apidocs/org/apache/xml/serializer/OutputPropertiesFactory.html – TWiStErRob 2016-08-08 11:10:52

+0

處提供的,http:// xml.apache.org/xslt是即使在舊版本中也不推薦使用(請參閱[聲明xalan命名空間](https://xalan.apache.org/old/xalan-j/extensions.html)),請使用http://xml.apache.org/xalan ' – TWiStErRob 2016-08-08 11:13:48

2

我想你必須將method設置爲xml。如果還是不行,請嘗試以下操作:

<?xml version="1.0" encoding="UTF-8"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan"> 

<xsl:output method="xml" encoding="UTF-8" indent="yes" xalan:indent-amount="4"/> 
+0

不幸的是它不起作用:( – Vincent 2010-03-08 20:21:44

+0

是否有可能您正在查看xml的應用程序無法正確呈現內容? – thelost 2010-03-09 07:23:55

+0

我在控制檯上輸出轉換 – Vincent 2010-03-09 14:29:18

5

爾卡-X1,謝謝你的問題-鏈接。我用下面的(與建議由愛德諾爾13/8/04):

<xsl:stylesheet ... xmlns:xslt="http://xml.apache.org/xslt"> 
<xsl:output ... indent="yes" xslt:indent-amount="4" /> 

這對我的作品與xalan的(JAVA)2.7.1。

8

與此掙扎了一會兒,但是剛剛纔意外地工作:

的關鍵是增加<xsl:strip-space elements="*"/>

所以它看起來就像這樣:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:java="http://xml.apache.org/xalan/java" 
    xmlns:xalan="http://xml.apache.org/xslt"> 
<xsl:output method="xml" encoding="ASCII" indent="yes" xalan:indent-amount="4"/> 
<xsl:strip-space elements="*"/> 

不知道爲什麼,但可能刪除所有的空格可以幫助xalan找出縮進

+0

沒有'xsl:strip-space [@elements =「*」]',xsl試圖保留輸出中輸入的空白​​節點。 – Timothy 2016-12-08 22:08:09