0

It's 2024, and I am working on a project that incorporates XSLT technology.

I came up with a simple way to access XML file that is being transoformed with javascript simply but outputing it to a hidden div, and then parsing it's content with XMLParser in JS.

<div class="hidden">
    <xsl:copy-of select="/*" />
</div>

Everything worked so far, I could access nodes with querySelector and attributes. Only problem I just noticed - if XML file has image node it is being converted to img

Something like that:

<div class="hidden">
    <object>
        <image>link</image>
    </object>
</div>

Converts to

<div class="hidden">
    <object>
        <img>link
    </object>
</div>

Which is completely wrong and breaks parsing. It only happens to this node. I don't really know what causes this - I am using IE11 compatability mode to transform XSLT stuff - any way to stop it from doing so? I obviously can't modify the XML since it comes from backend.

4
  • I would certainly not try to have the browser interpret XML as HTML and you do that when you put XML into an HTML (div) element. It is not clear why you need to do that and can't just fetch the XML source with XMLHttpRequest or fetch (if that is supported in IE). Commented Apr 3 at 9:39
  • So your problem is the XMLParser in JS? Could you share the JS-part? It should create a @src attribute with the value of the text inside image-tag Commented Apr 3 at 9:42
  • @MartinHonnen It's a local standalone kinda thing, I don't have any other way to access said XML. I'm not sure the software that I am developing even grants access to fetch and XMLHttpRequests. I am not sure it even has internet access. I mean I could access XML file via XSLT, but the point is I kinda trying to move away from it, since XSLT is old and bulky, and javascript is way better. Commented Apr 3 at 9:55
  • @SiebeJongebloed No, it's not JS thing. XSLT transformer outputs said XML with tags already replaced. Commented Apr 3 at 9:56

1 Answer 1

1

I don't think this has anything to do with XSLT, if you let an HTML parser parse markup like the one you showed then it parses into an HTML object element containing an HTML img element.

document.getElementById('test').innerHTML = `<object><image><\/image><\/object>`;
console.log(document.getElementById('test'));
console.log(document.getElementById('test').outerHTML);
<div id="test"></div>

So don't expect to be able to parse and preserve XML by parsing it as HTML5.

You can store XML in an HTML script element and read out its .text property:

var scriptElement = document.getElementById('test');

console.log(scriptElement.text);

var xmlDoc = new DOMParser().parseFromString(scriptElement.text, 'application/xml');

console.log(xmlDoc);
<script id="test" type="application/xml"><object><image></image></object></script>

6
  • VERY interesting. So it's not XSLT, and not JS as well (I disabled JS in browser to test my code) So I guess it's a thing that browsers do? And there is no workaround but to ask backend guys to change nodename to something like "picture" instead of "image" ? Commented Apr 3 at 10:34
  • Treat/parse XML as XML, don't parse it as HTML or stuff it into an HTML DOM. Commented Apr 3 at 10:55
  • I suppose. Is there any other way to expose XML file that only exists within XSLT framework to javascript on the webpage it generates otherwise? Commented Apr 3 at 11:06
  • It is possible to stuff XSLT/XML source code into an HTML <script type="application/xml"><object><image></image></object></script> element and read out the source XML/XSLT from the script element's .text property. That way to XML/XSLT source code is untouched/preserved. Commented Apr 3 at 11:13
  • My man this is EXACTLY what I was looking for. It is perfect! This should be the answer Commented Apr 3 at 11:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.