Remove newline character/white space from xml using xslt or xquery transformation

StackOverflow https://stackoverflow.com/questions/22803218

  •  26-06-2023
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top