How do I make xsl transformation indent the output

2019-01-26 06:25发布

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?

4条回答
放荡不羁爱自由
2楼-- · 2019-01-26 06:53

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"/>
查看更多
叼着烟拽天下
3楼-- · 2019-01-26 06:58

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-26 07:04

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

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-26 07:11

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"/>
查看更多
登录 后发表回答