Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive test is broken #116

Open
roesslerj opened this issue Dec 6, 2024 · 2 comments
Open

Interactive test is broken #116

roesslerj opened this issue Dec 6, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@roesslerj
Copy link

When opening the usage example and executing the transformation in the browser (Chrome), no result is shown and instead the following error message appears in the console:

Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
    at xslt.html:18
Uncaught ReferenceError: cleanxml is not defined
    at onload (xslt.html:34:29)

Even when fixing all obvious errors in the code, like so:

      function el(id) {
        return document.getElementById(id);
      }

      function test_xslt() {
          const xmlParser = new globalThis.XsltProcessor.XmlParser();
          const xml = xmlParser.xmlParse(el('xml').value);
          const xslt = xmlParser.xmlParse(el('xslt').value);
          const xsltProcessor = new globalThis.XsltProcessor.Xslt();
          xsltProcessor.xsltProcess(xml, xslt).then(output => {
            console.log(output);
            el('html').value = output;
            el('htmldisplay').innerHTML = output;
          });
      }

      function cleanxml() {
          cleanvalue('xml');
          cleanvalue('xslt');
      }

      function cleanvalue(id) {
          const x = el(id);
          x.value = x.value.replace(/^\s*/, '').replace(/\n\s*/g, '\n');
      }

output with the given XML and XSLT is '', which is wrong.

@leonelsanchesdasilva leonelsanchesdasilva self-assigned this Dec 6, 2024
@leonelsanchesdasilva leonelsanchesdasilva added the bug Something isn't working label Dec 6, 2024
@leonelsanchesdasilva
Copy link
Collaborator

leonelsanchesdasilva commented Dec 7, 2024

Hi @roesslerj 👋

Thanks for reporting the issue. For now, I'm fixing the interactive tests, but there's another bug that this issue helped me to understand.

The way this library works (and it's the exact way I've inherited it) is:

  • For each <xsl:apply-templates> found, runs all the <xsl:template> from the document until it is able to find one that brings child nodes from <xsl:apply-templates>;
  • Each call to <xsl:template> will try to rematch nodes;
  • In our examples:

XML:

<page>
    <message>
        Hello World.
    </message>
</page>

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet>
    <xsl:template match="/">
        <xsl:apply-templates select="page/message"/>
    </xsl:template>

    <xsl:template match="page/message">
        <div style="color:green">
            <xsl:value-of select="."/>
        </div>
    </xsl:template>
</xsl:stylesheet>

<xsl:apply-templates> selects <element> node. When called, <xsl:template match="page/message"> tries to select nodes starting by <element>, because the path is relative. Not finding any nodes, it returns an empty string.

There are many aspects I'll have to rewrite in this library, and this one should take me some time to resolve, since it's (very) likely I'm having to rewrite the entire XPath mechanism to calculate priorities correctly. Other aspect is about default implemented implicit default templates, as described here: https://stackoverflow.com/a/1976910/1314276

I'll keep this issue open as a reminder of what needs to be fixed and/or reimplemented.

@roesslerj
Copy link
Author

Thank you for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants