Search code examples
xmlxsltwhitespace

Keeping whitespace outside of root element with XSLT


I'm using XSLT to process an XML document that is under source control and must remain human readable.

As such, I'm trying to edit a text node, but keep the rest of the document exactly as is.

I'm finding that whitespace outside of the root element is being stripped. Specifically a newline between the XML declaration and the root element's open tag, and a newline at the end of the document.

When I add the indent=yes attribute to the xsl:output element, a newline does appear at the end of the document. However, the newline between the XML declaration and the root element's open tag is still missing.

What is my stylesheet missing?

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>

Source 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>

Expected Result — only text in /project/versionhas changed

<?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>

Actual Result

<?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>

Solution

  • If you are using Java, please try to add the following line:

    transformer.setOutputProperty("http://www.oracle.com/xml/is-standalone", "yes");