@mikezimm/fps-library-v2
Version:
Library of reusable typescript/javascript functions, interfaces and constants
23 lines • 1.16 kB
JavaScript
// This for function: https://stackoverflow.com/a/47317538
export function formatXml(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: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>',
].join('\n'), 'application/xml');
const xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
const resultDoc = xsltProcessor.transformToDocument(xmlDoc);
const resultXml = new XMLSerializer().serializeToString(resultDoc);
return resultXml;
}
//# sourceMappingURL=formatXml.js.map