Note that XSLT is planned to be removed from web browsers in late 2026, so this answer will stop working then.


This can be done using native javascript tools, without 3rd party libs, extending the @Dimitre Novatchev's answer:

var prettifyXml = function(sourceXml)
{
    var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
    var 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:template match="para[content-style][not(text())]">', // change to just text() to strip space in text nodes
        '    <xsl:value-of select="normalize-space(.)"/>',
        '  </xsl:template>',
        '  <xsl:template match="node()|@*">',
        '    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
        '  </xsl:template>',
        '  <xsl:output indent="yes"/>',
        '</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;
};

console.log(prettifyXml('<root><node/></root>'));

Outputs:

<root>
  <node/>
</root>

JSFiddle

Note, as pointed out by @jat255, pretty printing with <xsl:output indent="yes"/> is not supported by firefox. It only seems to work in chrome, opera and probably the rest webkit-based browsers.

Answer from Klesun on Stack Overflow
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-formatter
Best XML Formatter and XML Beautifier
Free XML Formatter also works as XML Converter / Convertor to JSON. Know more about XML: How to Print XML? Python XML Pretty Print ยท How to create XML File? Best and Secure XML Formatter works well in Windows, Mac, Linux, Chrome, Firefox, Safari, and Edge. Format XML File upload files and beautifies.
๐ŸŒ
Online XML Tools
onlinexmltools.com โ€บ prettify-xml
Prettify XML - Online XML Tools
Free online XML beautifier. Just load your XML and it will automatically get prettified. There are no ads, popups or nonsense, just an awesome XML prettifier. Load XML, prettify XML.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-pretty-print
Best XML Pretty Print Online
XML Pretty Print is very unique tool for prettify json and pretty print XML data in color.
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
Pretty XML - Visual Studio Marketplace
Extension for Visual Studio Code - XML formatter extension for Visual Studio Code. Formats XML documents just like Visual Studio.
๐ŸŒ
FreeFormatter
freeformatter.com โ€บ xml-formatter.html
Free Online XML Formatter - FreeFormatter.com
This free online XML formatter and lets you chose your indentation level and also lets you export to file
๐ŸŒ
Liquid Technologies
liquid-technologies.com โ€บ online-xml-formatter
Free Online XML Formatter
Formats an XML document indenting it to make it more readable, a process sometimes referred to as 'beautify' or 'prettify'.
Find elsewhere
๐ŸŒ
Chrome Web Store
chromewebstore.google.com โ€บ detail โ€บ xml-formatter โ€บ ejmpbcebpllmffkidemmlecpgboklcme
XML Formatter - Chrome Web Store
Format your XML so that it is indented correctly for easier reading. This formatter lets you pretty print your XML. Just paste your XML and then click the Format button. You can also open a local XML file to format. The formatter checks that your XML is well-formed.
Top answer
1 of 16
89

Note that XSLT is planned to be removed from web browsers in late 2026, so this answer will stop working then.


This can be done using native javascript tools, without 3rd party libs, extending the @Dimitre Novatchev's answer:

var prettifyXml = function(sourceXml)
{
    var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
    var 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:template match="para[content-style][not(text())]">', // change to just text() to strip space in text nodes
        '    <xsl:value-of select="normalize-space(.)"/>',
        '  </xsl:template>',
        '  <xsl:template match="node()|@*">',
        '    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
        '  </xsl:template>',
        '  <xsl:output indent="yes"/>',
        '</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;
};

console.log(prettifyXml('<root><node/></root>'));

Outputs:

<root>
  <node/>
</root>

JSFiddle

Note, as pointed out by @jat255, pretty printing with <xsl:output indent="yes"/> is not supported by firefox. It only seems to work in chrome, opera and probably the rest webkit-based browsers.

2 of 16
62

From the text of the question I get the impression that a string result is expected, as opposed to an HTML-formatted result.

If this is so, the simplest way to achieve this is to process the XML document with the identity transformation and with an <xsl:output indent="yes"/> instruction:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

When applying this transformation on the provided XML document:

<root><node/></root>

most XSLT processors (.NET XslCompiledTransform, Saxon 6.5.4 and Saxon 9.0.0.2, AltovaXML) produce the wanted result:

<root>
  <node />
</root>
๐ŸŒ
BeautifyTools
beautifytools.com โ€บ xml-beautifier.php
Online XML Beautifier - XML Formatter - BeautifyTools.com
Online XML Beautifier beautifies ugly, minified xml code and makes it more readable. It gives the code proper indentation, spaces and newlines and makes it well-formatted code.
๐ŸŒ
Teleport
goteleport.com โ€บ resources โ€บ tools โ€บ xml-beautifier
XML Beautifier | Instantly Format & Beautify XML | Teleport
Instantly format and beautify XML data with this free online tool. Improve readability and validate your XML with just a few clicks.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ xml formatter
XML Formatter and XML Beautifier Online
XML Formatter Online - Our free XML Beautifier tool enables you to format XML data, aiding in editing, viewing, and analyzing XML data.
๐ŸŒ
Web Toolkit Online
webtoolkitonline.com โ€บ xml-formatter.html
XML Formatter online
Secure Free XML Formatter - Process your XML code entirely in your browser with no data transfers.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-pretty-print โ€บ ffde1b
XML Pretty Print
XML Pretty Print is very unique tool for prettify json and pretty print XML data in color.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ xml-pretty-print
XML Pretty Print
XML Pretty Print is easy to use tool to pretty print XML String.
๐ŸŒ
OneLogin
developers.onelogin.com โ€บ saml โ€บ online-tools โ€บ xml-pretty-print
XML Pretty Print Online - XMPrettyPrint SAML Online Tool
XML Pretty Print ยท This tool lets you present the XML of a SAML Message in a human-readable format. Clear Form Fields ยท Found a problem or a bug? Submit a support ticket. Looking for walkthroughs or how-to guides on OneLogin's user and admin ...
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ bbedit โ€บ c โ€บ Uu3d0Q6tnuM
XML Pretty Print
You should have a program called xmllint in your /usr/bin/ directory that can parse and reformat an xml file, stream or URL. Since it can process standard input and output to standard out, it can be used in a text filter. See the user manual and http://bbeditextras.org/wiki/index.php?title=Text_Filters for more information on how to create and use text filters.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-parser
Best XML Parser Online
Python XML Pretty Print ยท How to create XML File? Best and Secure Online XML Parser works well in Windows, Mac, Linux, Chrome, Firefox, Safari and Edge. FAQ ยท XML Parser is very unique tool for XML formatting, converting to XML, CSV. It can be used as XML validator, XML editor and XML Parser.
๐ŸŒ
Notepad++ Community
community.notepad-plus-plus.org โ€บ topic โ€บ 22501 โ€บ xml-plugin-pretty-print
XML Plugin - Pretty Print | Notepad++ Community
February 7, 2022 - Then i saw I was in 64bit, so I unzipped a 32-bit to match yours, and installed those three plugins. Plugins > XML Tools > Pretty Print still only affects the active XML I even opened a second XML file, and ran the Pretty Print in the first: it still only affects the active XML, and not the second XML or the JSON.