Features

Prettify XML Online

Paste the XML in the textarea below. TestingBot will automatically prettify the XML and output it in the second textarea.

Enter value

Output

What is XML Prettify Tool?

TestingBot offers a free tool to clean up and indent XML code. TestingBot will make sure the indentation of all XML code is correct.

What are the benefits of using XML Prettify Online?

Using XML prettify, or formatting XML in a human-readable and well-organized manner, offers several benefits that make working with XML documents easier and more efficient:

  • Improved Readability:

    Prettified XML is easier to read and understand, especially when dealing with complex XML structures. Proper indentation and line breaks make the hierarchy and nesting of elements and attributes clear, enhancing readability.

  • Debugging and Troubleshooting:

    Prettified XML helps developers quickly identify issues and errors in the XML code. When an error occurs, it becomes easier to pinpoint the problematic section, allowing for faster debugging and troubleshooting.

  • Version Control:

    When using version control systems like Git, prettified XML allows for cleaner and more informative changes in version history. This helps developers understand the evolution of the XML document over time.

How to prettify XML in JavaScript?

You can prettify XML in Javascript by using DOMParser() and XSLTProcessor, see the example below:

const prettifyXml = function(sourceXml) {
  const xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
  const xsltDoc = new DOMParser().parseFromString([
      // describes how we want to modify the XML - indent everything
      '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
      '  <xsl:strip-space elements="*"></xsl:strip-space>',
      '  <xsl:template match="para[content-style][not(text())]">', // change to just text() to strip space in text nodes
      '    <xsl:value-of select="normalize-space(.)"></xsl:value-of>',
      '  </xsl:template>',
      '  <xsl:template match="node()|@*">',
      '    <xsl:copy><xsl:apply-templates select="node()|@*"></xsl:apply-templates></xsl:copy>',
      '  </xsl:template>',
      '  <xsl:output indent="yes"></xsl:output>',
      '</xsl:stylesheet>',
  ].join('\n'), 'application/xml');

  var xsltProcessor = new XSLTProcessor();
  xsltProcessor.importStylesheet(xsltDoc);
  var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
  var resultXml = new XMLSerializer().serializeToString(resultDoc);
  return resultXml;
}