我正在使用 XSLT 处理受源代码控制且必须保持人类可读性的 XML 文档。
因此,我正在尝试编辑文本节点,但保持文档的其余部分原样。
我发现根元素之外的空白被剥离了。特别是 XML 声明和根元素的打开标记之间的换行符,以及文档末尾的换行符。
当我将indent=yes
属性添加到xsl:output
元素时,文档末尾会出现换行符。但是,XML 声明和根元素的打开标记之间的换行符仍然缺失。
我的样式表缺少什么?
XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:param name="newVersion"/>
<xsl:template match="/project/version/text()">
<xsl:value-of select="$newVersion" />
</xsl:template>
</xsl:stylesheet>
源 XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.nuance</groupId>
<artifactId>parent-test-repo</artifactId>
<version>0.1.0</version>
</parent>
<artifactId>test-repo-1</artifactId>
<name>test-repo-1</name>
<version>1.1.0</version>
</project>
预期结果 - 只有文本/project/version
发生了变化
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.nuance</groupId>
<artifactId>parent-test-repo</artifactId>
<version>0.1.0</version>
</parent>
<artifactId>test-repo-1</artifactId>
<name>test-repo-1</name>
<version>2.0.0</version>
</project>
实际结果
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.nuance</groupId>
<artifactId>parent-test-repo</artifactId>
<version>0.1.0</version>
</parent>
<artifactId>test-repo-1</artifactId>
<name>test-repo-1</name>
<version>2.0.0</version>
</project>
如果您使用的是 Java,请尝试添加以下行:
transformer.setOutputProperty("http://www.oracle.com/xml/is-standalone", "yes");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句