XSLT stylesheet provided is not producing the desired indentation in Firefox

51 views Asked by At

The XSLT code provided below is not producing the desired indentation in the output of XML when I am using Firefox but it is working fine for chrome.

Here is the code:

var sourceXml = '<Environment name=“DEFAULT”>\n<ADFConnections>\n<Connection name="MapViewer”>\n<Attribute name="url”>\n</Connection>\n</ADFConnections>\n</Environment>'
      var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
      var xsltDoc = new DOMParser().parseFromString([
            '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
            '  <xsl:output indent="yes"/>',
            '  <xsl:strip-space elements="*"/>',
            '  <xsl:template match="node()|@*">',
            '    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
            '  </xsl:template>',
            '</xsl:stylesheet>',
        ].join('\n'), 'application/xml'); 

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

In FireFox, it was showing like in single line

<Environment name="DEFAULT"><ADFConnections><Connection name="MapViewer”> .....

without any indentation. What I was doing wrong here where as it is working as expected in chrome with indentation.

1

There are 1 answers

0
Martin Honnen On

To show you how you can use XSLT 3 with SaxonJS 2 and control the serialization in a cross-browser compatible way (note that the input XML you posted didn't seem to be well-formed XML so I corrected it a bit):

const sourceXml = `<Environment name="DEFAULT">
  <ADFConnections>
    <Connection name="MapViewer">
      <Attribute name="url"></Attribute>
    </Connection>
  </ADFConnections>
</Environment>`;

const xsltCode = `<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
  <xsl:param name="indent" as="xs:boolean" static="yes" select="true()"/>
  <xsl:output _indent="{$indent}"/>
  <xsl:strip-space elements="*"/>
  <xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>`;

const result1 = SaxonJS.XPath.evaluate(`transform(map{'stylesheet-text' : $xslt-code, 'source-node' : parse-xml($xml), 'delivery-format' : 'serialized' })?output`, null, { params: { 'xslt-code' : xsltCode, 'xml' : sourceXml } });

console.log(result1);

const result2 = SaxonJS.XPath.evaluate(`transform(map{'stylesheet-text' : $xslt-code, 'source-node' : parse-xml($xml), 'delivery-format' : 'serialized', 'static-params' : map { QName('', 'indent') : false() } })?output`, null, { params: { 'xslt-code' : xsltCode, 'xml' : sourceXml } });

console.log(result2);
<script src="https://martin-honnen.github.io/SaxonJS-2.6/SaxonJS2.js"></script>