Saxon-HE不同版本及操作系统下的XML缩进差异问询
Saxon-HE跨操作系统XML缩进差异问题分析
现象说明
使用相同的Java代码、XSLT模板及Saxon-HE 11.2版本时,XSLT转换后的XML缩进会因操作系统不同产生差异:
Ubuntu 22.04下的输出
<isee:ISEE-Event xmlns:ex="http://www.example.com/schemas/ex/5.00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaVersion="5.00">
Debian 10与RedHat下的输出
<isee:ISEE-Event xmlns:ex="http://www.example.com/schemas/ex/5.00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaVersion="5.00">
可见Debian 10和RedHat输出的第二、第三行比Ubuntu的多了几个空格。另外:
- Saxon-HE 9.x版本不存在这种跨系统差异
- Saxon-HE 12.x版本无论在哪个操作系统下,都会统一添加这部分额外空格
相关代码与输入
XSLT模板
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> <xsl:template match="/"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet>
XML输入
<?xml version="1.0" encoding="UTF-8"?> <ex:root xmlns:ex="example"> <ex:Property name="property-main-1" value="A pretty long value for an attribute. But we need to test the indentation as well"/> <ex:Property name="property-sub1-1" value="I'm the property from sub1.xml"/> <ex:Property name="property-sub2-1" value="I'm the property from sub2.xml"/> </ex:root>
原因解析
这个差异本质是Saxon-HE不同版本对XML属性换行缩进的实现逻辑变更:
- Saxon 9.x:缩进算法较为简单,仅采用固定空格数缩进,不会根据元素名称长度调整属性换行后的位置,因此跨系统表现一致。
- Saxon 11.x:开发团队调整了缩进逻辑,目的是让属性换行后与起始标签的属性起始位置对齐,提升可读性。但该版本的缩进计算依赖系统环境(如JDK字符宽度处理、底层排版设置),导致不同操作系统下的缩进空格数出现差异。
- Saxon 12.x:修复了11.x版本的系统依赖问题,统一了缩进计算规则,不再受操作系统环境影响,因此所有系统下的输出缩进保持一致。
内容的提问来源于stack exchange,提问作者gabe
相关产品推荐
相关产品推荐

