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

CDATA in xsl not working as expected #95

Closed
Kartonatic opened this issue May 31, 2024 · 3 comments
Closed

CDATA in xsl not working as expected #95

Kartonatic opened this issue May 31, 2024 · 3 comments
Assignees

Comments

@Kartonatic
Copy link

Kartonatic commented May 31, 2024

Hi!
I'm trying to put js code in the xsl template but it is not able to resolve the CDATA tag within the xsl. You could use this code to test:

import { XmlParser, Xslt, XsltOptions } from 'xslt-processor';

// Transform template to HTML
const xsl = `
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" version="4.0" encoding="utf-8" omit-xml-declaration="yes" />
  <xsl:template match="/">
    <html>
      <head>
        <script type="text/javascript">
          <![CDATA[
          if (1 < 2) {}
        ]]>
        </script>
      </head>
      <body>
        <h1>
          <xsl:value-of select="ReportName"/>
        </h1>
        <p>
          <xsl:value-of select="GenerationDate"/>
        </p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
    `;
const xml = `
<XampleXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ReportName>My first transformation in js</ReportName>
  <GenerationDate>01/06/2024</GenerationDate>
</XampleXml>
    `;

const options: XsltOptions = {
  cData: true,
  escape: false,
  selfClosingTags: false,
};
const xslt = new Xslt(options);
const xmlParser = new XmlParser();
xslt
  .xsltProcess(xmlParser.xmlParse(xml), xmlParser.xmlParse(xsl))
  .then((output) => {
    console.log(output);
  });
//OUTPUT: <html><head><script type="text/javascript"></script></head><body><h1>My first transformation in js</h1><p>01/06/2024</p></body></html>

As you can see, the content of the script (CDATA content) does not appear in the output.

Another curious thing is that if we add xsl:text around it, the following happens:

import { XmlParser, Xslt, XsltOptions } from 'xslt-processor';

// Transform template to HTML
const xsl = `
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" version="4.0" encoding="utf-8" omit-xml-declaration="yes" />
  <xsl:template match="/">
    <html>
      <head>
        <script type="text/javascript">
          <xsl:text disable-output-escaping="yes">
            <![CDATA[
          if (1 < 2) {}
        ]]>
          </xsl:text>
        </script>
      </head>
      <body>
        <h1>
          <xsl:value-of select="ReportName"/>
        </h1>
        <p>
          <xsl:value-of select="GenerationDate"/>
        </p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
    `;
const xml = `
<XampleXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ReportName>My first transformation in js</ReportName>
  <GenerationDate>01/06/2024</GenerationDate>
</XampleXml>
    `;

const options: XsltOptions = {
  cData: true,
  escape: false,
  selfClosingTags: false,
};
const xslt = new Xslt(options);
const xmlParser = new XmlParser();
xslt
  .xsltProcess(xmlParser.xmlParse(xml), xmlParser.xmlParse(xsl))
  .then((output) => {
    console.log(output);
  });
//OUTPUT:
//
//
//
// if (1 < 2) {}
//
// <html><head><script type="text/javascript"></script></head><body><h1>My first transformation in js</h1><p>01/06/2024</p></body></html>

The script appears at the beginning of the resulting html.

Could you investigate to resolve that?

Thank you!

@leonelsanchesdasilva leonelsanchesdasilva self-assigned this Jun 28, 2024
@leonelsanchesdasilva
Copy link
Collaborator

Hi @Kartonatic. Thanks for reporting.

I should be able to return to this issue in the next weeks.

@leonelsanchesdasilva
Copy link
Collaborator

@Kartonatic I'm working on this now. I got to simulate your problem.

I'll let you know when I got to fix it here.

leonelsanchesdasilva added a commit that referenced this issue Sep 9, 2024
- `output` node should only be expected for fragments;
- While transforming source elements, attributes can be 1) created by a previous transformation, and 2) previous attributes could be renamed.
@leonelsanchesdasilva
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants