Question

How to remove newline character/white space from xml using xslt or xquery transformation.Means all the elements will come in a single line.

Was it helpful?

Solution

In xslt, you can add a top level element

<xsl:strip-space elements="*"/>

By using a sample input XML below:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <a>XXX</a>
    <b>YYY</b>
    <c>ZZZ</c>
</root>

and the following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>

    <!-- this is called an identity template -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

produces:

<?xml version="1.0" encoding="utf-8"?><root><a>XXX</a><b>YYY</b><c>ZZZ</c></root>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top