我在寻找一些能够正确缩进我的xml文档的XSLT样式表,并在http://www.printk.net/~bds/indent.html上找到了一个非常好的样式表。希望作者不会责备我引用他的内容。
除了一个讨厌的问题,它几乎完成了我想要的所有功能:它在我的文档根元素(但不包括 XML 声明)上制作出奇怪的 8 个空格缩进。结果看起来像这样一段标记:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<xsl:param name="indent-increment" select="' '"/>
<xsl:template name="newline">
<xsl:text disable-output-escaping="yes">
</xsl:text>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="text()[normalize-space(.)='']"/>
<xsl:template match="*">
<xsl:param name="indent" select="''"/>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
<xsl:choose>
<xsl:when test="count(child::*) > 0">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="*|text()">
<xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
</xsl:apply-templates>
<xsl:call-template name="newline"/>
<xsl:value-of select="$indent"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
除了一个讨厌的问题,它几乎完成了我想要的所有功能:它在我的文档根元素(但不包括 XML 声明)上制作出奇怪的 8 个空格缩进。结果看起来像这样一段标记:
<?xml version="1.0" encoding="UTF-8"?>
<database>
<books>
<book id="0">
<ISBN value="0123456789"/>
<title>Some book title Language</title>
<hardcover value="false"/>
<price value="40.46"/>
<in_stock value="100"/>
<annotation>Some annotation</annotation>
</book>
</books>
</database>`
我对XSLT技术还比较陌生,花了很多时间尝试理解如何修复它,但目前为止还没有成功。我正在使用标准的javax.xml.transform.Transformer类进行XSLT转换。你有任何想法是为什么会发生这种情况吗?