代码之家  ›  专栏  ›  技术社区  ›  Vincent

如何使xsl转换缩进输出?

  •  11
  • Vincent  · 技术社区  · 14 年前

    我使用的Xalan具有以下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"/>
    

    输出没有缩进。

    有人有主意吗?

    4 回复  |  直到 14 年前
        1
  •  21
  •   Jirka    13 年前

    对于缩进,需要使用其他命名空间:“ 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"/>
    
        2
  •  9
  •   tevch    11 年前

    在这件事上挣扎了一段时间,却意外地成功了:

    关键是 <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找出缩进

        3
  •  5
  •   Eugen Labun rbrayb    13 年前

    Jirka-X1,谢谢你的问题链接。我使用了以下内容(如2004年8月13日Ed Knoll提出的):

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

    这对我来说是用Java()的。

        4
  •  2
  •   thelost    14 年前

    我想你得把 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"/>