How do I make xsl transformation indent the output?

ぐ巨炮叔叔 提交于 2019-11-28 02:57:42

问题


I'm using xalan with the following xsl header:

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

And the output is not indented.

Anyone with ideas?


回答1:


For indentation you need to use a different namespace: "http://xml.apache.org/xslt" (see 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"/>



回答2:


Was struggling with this for a while, however just got it working accidentally:

the key was to add <xsl:strip-space elements="*"/>

so it will look like this:

<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="*"/>

Not sure why, but probably removing all whitespacing helps xalan figure out the indentation




回答3:


Jirka-x1, thank you for the issue-link. I used the following (as proposed by Ed Knoll 13/Aug/04):

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

This works for me with xalan (java) 2.7.1.




回答4:


I guess you have to set the method to xml. If that does not work, try the following:

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


来源:https://stackoverflow.com/questions/2402212/how-do-i-make-xsl-transformation-indent-the-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!