我在这里想念什么?
我认为不应该弄乱 XSLT 处理器的默认缩进。
大多数情况下,<xsl:output indent="yes"/>
和的组合<xsl:strip-space elements="*"/>
足以获得良好的缩进。
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="unit">
<unit>
<category1>
<xsl:apply-templates select="*[not(position() >2)]"/>
</category1>
<category2>
<xsl:apply-templates select="*[position() >2]"/>
</category2>
</unit>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<list>
<unit>
<data1>a</data1>
<data2>b</data2>
<data3>c</data3>
</unit>
</list>
产生想要的、缩进良好的结果:
<list>
<unit>
<category1>
<data1>a</data1>
<data2>b</data2>
</category1>
<category2>
<data3>c</data3>
</category2>
</unit>
</list>
当使用以下七个 XSLT 处理器中的任何一个运行转换时,都会产生相同的结果:
AltovaXML (XML-SPY)。
.NET XslCompiledTransform。
.NET XslTransform。
撒克逊 6.5.4。
Saxon 9.1.05(XSLT 2.0 处理器)。
XQSharp/XMLPrime(XSLT 2.0 处理器)。
AltovaXml(用于 XSLT 2.0)。
MSXML3/4/6 的情况更复杂——这些 XSLT 处理器的缩进只包含一个换行符,因此每个元素都在一个新行上,但出现在行首。
对于这些 XSLT 处理器,我使用以下两遍处理,第一遍是上述转换,第二遍适用于Nikolai Grigoriev 提出的 XML 漂亮打印机的第一遍结果,可在维护的XSLT 常见问题解答站点中找到戴夫·鲍森:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="ext">
<xsl:output method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:param name="indent-increment" select="' '" />
<xsl:variable name="vrtfPass1">
<xsl:apply-templates select="/*"/>
</xsl:variable>
<xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$vPass1/*" mode="pass2"/>
</xsl:template>
<xsl:template match="unit">
<unit>
<category1>
<xsl:apply-templates select="*[not(position() >2)]"/>
</category1>
<category2>
<xsl:apply-templates select="*[position() >2]"/>
</category2>
</unit>
</xsl:template>
<xsl:template match="*" mode="pass2">
<xsl:param name="indent" select="'
'"/>
<xsl:value-of select="$indent"/>
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates mode="pass2">
<xsl:with-param name="indent"
select="concat($indent, $indent-increment)"/>
</xsl:apply-templates>
<xsl:value-of select="$indent"/>
</xsl:copy>
</xsl:template>
<xsl:template match="comment()|processing-instruction()" mode="pass2">
<xsl:copy />
</xsl:template>
<!-- WARNING: this is dangerous. Handle with care -->
<xsl:template match="text()[normalize-space(.)='']" mode="pass2"/>
</xsl:stylesheet>
当对同一个(提供的)XML 文档(上图)执行此转换时,生成的结果具有所需的缩进:
<?xml version="1.0" encoding="UTF-16"?>
<list>
<unit>
<category1>
<data1>a
</data1>
<data2>b
</data2>
</category1>
<category2>
<data3>c
</data3>
</category2>
</unit>
</list>
这些都是我计算机上的 XSLT 处理器。我建议尝试最后的转换——它很可能会用 Xalan-C 产生想要的结果。
请注意:
最后一个转换使用 MSXML 特定的扩展函数 xxx:node-set(),属于 MSXML 特定的命名空间:
xmlns:ext="urn:schemas-microsoft-com:xslt"
对于 Xalan,这需要替换为:
xmlns:ext="http://exslt.org/common"
或者,如果不支持 EXSLT,则使用本机 Xalan 命名空间:
xmlns:ext="http://xml.apache.org/xalan
在最后一种情况下,对ext:node-set()
函数的调用必须替换为对ext:nodeset()
(注意缺少的破折号)的调用:
<xsl:variable name="vPass1" select="ext:nodeset($vrtfPass1)"/>