11 Comments

 

clip_image002

 

This issue can be fixed in IE9 by using the following javascript function:

 

/*
Transforms a XML document to a HTML string by using a XSLT document
1. Use type XSLTProcessor, if browser (FF, Safari, Chrome etc) supports it
2. Use function [transformNode] on the XmlDocument, if browser (IE6, IE7, IE8) supports it
3. Use function transform on the XsltProcessor used for IE9 (which doesn't support [transformNode] any more) 
4. Throws an error, when both types are not supported
*/
function TransformToHtmlText(xmlDoc, xsltDoc) {
    // 1.
    if (typeof (XSLTProcessor) != "undefined") {
        var xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(xsltDoc);
        var xmlFragment = xsltProcessor.transformToFragment(xmlDoc, document);
        return GetXmlStringFromXmlDoc(xmlFragment);
    }
    // 2.
    if (typeof (xmlDoc.transformNode) != "undefined") {
        return xmlDoc.transformNode(xsltDoc);
    }
    else {

        try {
            // 3
            if (window.ActiveXObject) {
                var xslt = new ActiveXObject("Msxml2.XSLTemplate");
                var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
                xslDoc.loadXML(xsltDoc.xml);
                xslt.stylesheet = xslDoc;
                var xslProc = xslt.createProcessor();
                xslProc.input = xmlDoc;
                xslProc.transform();

                return xslProc.output;
            }
        }
        catch (e) {
            // 4
            alert("The type [XSLTProcessor] and the function [XmlDocument.transformNode] are not supported by this browser, can't transform XML document to HTML string!");
            return null;
        }

    }
}

11 Replies to “How to fix: DOMParser.TransformNode not supported in IE9”

  1. Hi, nice article. I just wonder where the GetXmlStringFromXmlDoc function is defined? Is it native somewhere or did you forget to include that one?

    Regards,

    Jörgen

  2. Hi,
    I have successfully transform XML to XSL for IE9 by modified some minor changes in above function.
    But for following line:

    document.all(“myIsland”).XMLDocument.selectSingleNode(“//element[@id=’123456789′]/descendant::element[@mn=’subchapitre’ and not(@chg = ‘deleted’) and not(@display=’false’)]/@id”);

    give js error like :

    Error: Expected token ‘eof’ found ‘:’.
    //element[@id=’123456789′]/descendant–>:<–:element[@mn='subchapitre' and not(@chg = 'deleted') and not(@display='false')]/@id.

    Can anyone help me??

    Any Help Greatly appreciated.Thanks in advance….

  3. @Roel
    Thanks man – exactly what I needed (almost!)

    Maybe you can update the title of the post a little to help others?
    This problem exists IE9 and greater – I’m specifically experiencing it with IE11 (beta o__O)

    @Jörgen

    I found this function as it was didn’t work in Chrome, so I used the XMLSerializer:
    So for example:

    if (typeof(GetXmlStringFromXmlDoc)!= “undefined”)
    {
    return GetXmlStringFromXmlDoc(xmlFragment);
    }
    else
    {
    // chrome friendly

    // get a xml serializer object
    var xmls = new XMLSerializer();

    // convert dom into string
    var sResult = xmls.serializeToString(xmlFragment);
    //extract contents of transform iix node if it is present
    if (sResult.indexOf(” -1)
    {
    sResult = sResult.substring(sResult.indexOf(“>”) + 1, sResult.lastIndexOf(“<"));
    }
    return sResult;
    }

    The revised function is now:

    function TransformToHtmlText(xmlDoc, xsltDoc)
    {
    // 1.
    if (typeof (XSLTProcessor) != "undefined")
    {
    var xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xsltDoc);
    var xmlFragment = xsltProcessor.transformToFragment(xmlDoc, document);

    if (typeof(GetXmlStringFromXmlDoc)!= "undefined")
    {
    return GetXmlStringFromXmlDoc(xmlFragment);
    }
    else
    {
    // chrome friendly

    // get a xml serializer object
    var xmls = new XMLSerializer();

    // convert dom into string
    var sResult = xmls.serializeToString(xmlFragment);
    //extract contents of transform iix node if it is present
    if (sResult.indexOf(" -1)
    {
    sResult = sResult.substring(sResult.indexOf(“>”) + 1, sResult.lastIndexOf(“<"));
    }
    return sResult;
    }
    }
    // 2.
    if (typeof (xmlDoc.transformNode) != "undefined")
    {
    return xmlDoc.transformNode(xsltDoc);
    }
    else {

    var activeXOb = null;
    try { activeXOb = new ActiveXObject("Msxml2.XSLTemplate"); } catch (ex) {}

    try {
    // 3
    if (activeXOb)
    {
    var xslt = activeXOb;
    var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
    xslDoc.loadXML(xsltDoc.xml);
    xslt.stylesheet = xslDoc;
    var xslProc = xslt.createProcessor();
    xslProc.input = xmlDoc;
    xslProc.transform();

    return xslProc.output;
    }
    }
    catch (e)
    {
    // 4
    alert("The type [XSLTProcessor] and the function [XmlDocument.transformNode] are not supported by this browser, can't transform XML document to HTML string!");
    return null;
    }

    }
    }

  4. This will work in IE11 too, if you change:
    if (window.ActiveXObject)

    to:
    if (window.ActiveXObject || “ActiveXObject” in window)

  5. I tried with this. But the output HTML has \r\n in it. I need to display the HTML within an element.

    This transform returns whole HTML i.e. including and and converting it into JQuery object does not yield the result due to \r\n.

  6. IE-11 still not working after changing to if (window.ActiveXObject || “ActiveXObject” in window).

    Please provide solutions

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

.NET 2.2 => NET 5 – Fix – The call is ambiguous between the following methods or properties: ‘ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[])’ and ‘ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Type[])’

0 Comments

  To fix this error I followed: https://www.thecodebuzz.com/configure-automapper-asp-net-core-profile-map-object/   services.AddAutoMapper(typeof(Startup).Assembly);