0

I am experiencing an issue with transforming XML data using XSLT in JavaScript. My setup involves fetching XML data and an XSLT stylesheet, which is embedded within a tag in my HTML. When I try to transform the XML using XSLTProcessor, the result is null. This issue occurs when I access the XSLT from the tag, but not when I directly assign the XSLT as a string in my JavaScript code.

html:

<script id="xslt-code" type="text/xsl"> <!-- XSLT content here --> </script>

js:

    async function fetchXMLAndApplyXSLT() {
    // Fetch XML data...
    const xsltCode = document.getElementById("xslt-code").textContent;
    const xslParser = new DOMParser();
    const xslStylesheet = xslParser.parseFromString(xsltCode, 'text/xml');
    const xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xslStylesheet);
    const transformedXML = xsltProcessor.transformToFragment(xmlDoc, document);

    if (!transformedXML) {
        console.error('Transformation resulted in null');
    }
    // Further processing...
}

Attempted Solutions:

  1. Ensured that the script tag has type="text/xsl".
  2. Checked that the script is executed after the DOM is fully loaded.
  3. Confirmed that textContent of the script tag returns the correct XSLT string.

Despite these efforts, transformedXML is still null. Directly assigning the XSLT as a string in JavaScript works fine, but fetching it from the tag doesn't.

Could anyone suggest what might be causing this issue or how to resolve it?

4
  • It is all client-side HTML and JavaScript, so just add an executable code sample with minimal but complete code demonstrating the issue. Is that happening for all your XSLT code or just for some particular XSLT code? Does that XSLT code run without problems in the browser if you load it from an xml-stylesheet processing instruction? Commented Dec 20, 2023 at 16:51
  • Does using .text html.spec.whatwg.org/multipage/scripting.html#dom-script-text instead of .textContent fix it? Commented Dec 20, 2023 at 16:55
  • @MartinHonnen sadly not. It seems like the parsing of the XSLT fails, if not directly loaded as a string: convertXML.js:29 Parsed XSLT-Stylesheet: #document <html>​<body>​<parsererror style=​"display:​ block;​ white-space:​ pre;​ border:​ 2px solid #c77;​ padding:​ 0 1em 0 1em;​ margin:​ 1em;​ background-color:​ #fdd;​ color:​ black">​…​</parsererror>​</body>​</html>​ Commented Dec 20, 2023 at 17:02
  • Well, embedding XSLT, which is XML, in HTML, to then read it out with all XML syntax details preserved, might be hard; the parse error should indicate which part got mangled. Commented Dec 20, 2023 at 17:05

1 Answer 1

0

Removing the linebreak between <script ...> and <?xml version="1.0" encoding="UTF-8"?> fixed the error for me

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.