Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined: undefined error when calling XSLTProcessor.prototype.importStylesheet

I want to prettify some XML, and I found following code (in this answer, JSFiddle). I modified it like this:

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:output omit-xml-declaration="yes" indent="yes"/>',
    '    <xsl:template match="node()|@*">',
    '      <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
    '    </xsl:template>',
    '</xsl:stylesheet>',
].join('\n'), 'application/xml');
function prettifyXml(sourceXml) {
    var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
    var xsltProcessor = new XSLTProcessor();
    // Error happens here:
    xsltProcessor.importStylesheet(xsltDoc);
    var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
    var resultXml = new XMLSerializer().serializeToString(resultDoc);
    return resultXml;
};

I get an error running the code, but the error has no message. It looks like this in Firefox console:

enter image description here

And this is what I see in debugger:

enter image description here

The error does also happen in the Fiddle from the original answer. I would like to know what kind of error is this and how to fix it.

I am including Firefox tag, because I think this is not a normal browser behavior. My version is 61.0.2 (64-bit).

like image 295
Tomáš Zato - Reinstate Monica Avatar asked Oct 21 '25 03:10

Tomáš Zato - Reinstate Monica


1 Answers

In terms of XSLT you need a version attribute on the root element of the stylesheet so try <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">. This seems to fix the error on the importStylesheet call, see https://jsfiddle.net/sgeryvyu/361/.

On the other hand, Firefox/Mozilla's XSLT processor is known to do a tree to tree transformation so with transformToDocument and Mozilla you won't get any serialization options of xsl:output applied, meaning that attempt to push your DOM tree through an XSLT simply gives you another DOM tree without the wanted indentation.

like image 150
Martin Honnen Avatar answered Oct 24 '25 07:10

Martin Honnen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!