Created
October 14, 2022 08:35
-
-
Save edwinm/ee1a4aec5c0a15741a8affcb56805e39 to your computer and use it in GitHub Desktop.
Pretty Print XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prettyXml(xml: string) { | |
try { | |
const domParser = new DOMParser(); | |
const xmlDom = domParser.parseFromString(xml, 'application/xml'); | |
const xsltDom = domParser.parseFromString( | |
`<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform> | |
<xsl:strip-space elements="*"/> | |
<xsl:template match="para[content-style][not(text())]"> | |
<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>`, | |
'application/xml', | |
); | |
const xsltProcessor = new XSLTProcessor(); | |
xsltProcessor.importStylesheet(xsltDom); | |
const resultDoc = xsltProcessor.transformToDocument(xmlDom); | |
xml = new XMLSerializer().serializeToString(resultDoc); | |
} catch (e) {} | |
return xml; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment