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

xsl:apply-templates for nodes inside text causes the text to print in wrong order #108

Open
jrutila opened this issue Oct 15, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@jrutila
Copy link

jrutila commented Oct 15, 2024

The buggy case is presented in this unit test code.

Basically, the <xsl:apply-templates> works but results in following output

<div><h2>test1</h2><p> hello<span>replaced text</span></p></div>

So the beginning of the text ("This is") is lost somewhere, the "hello" comes first and then the, correctly templated, "replaced text". If you remove the " hello" from the end, it works correctly.

    it('XSLT apply-template inside text test', async () => {
        const xmlString = `<root>
          <test name="test1">This is <repl>text</repl> hello</test>
        </root>`;

        const xsltString = `<?xml version="1.0"?>
          <xsl:stylesheet version="1.0">
            <xsl:template match="repl">
              <span>replaced <xsl:value-of select="." /></span>
            </xsl:template>
            <xsl:template match="/">
              <div>
                <h2><xsl:value-of select="test/@name" /></h2>
                <p><xsl:apply-templates select="test/node()" /></p>
              </div>
            </xsl:template>
          </xsl:stylesheet>`;

        const expectedOutString = `<div><h2>test1</h2><p>This is <span>replaced text</span> hello</p></div>`;

        const xsltClass = new Xslt();
        const xmlParser = new XmlParser();
        const xml = xmlParser.xmlParse(xmlString);
        const xslt = xmlParser.xmlParse(xsltString);

        const outXmlString = await xsltClass.xsltProcess(xml, xslt);

        assert.equal(outXmlString, expectedOutString);
    });
@leonelsanchesdasilva leonelsanchesdasilva self-assigned this Oct 15, 2024
@leonelsanchesdasilva leonelsanchesdasilva added the bug Something isn't working label Oct 15, 2024
@leonelsanchesdasilva
Copy link
Collaborator

@jrutila Thanks for reporting. I implemented your unit test.

I tested your example in an online XSLT transformer and my results were a bit different:

<div>
   <h2/>
   <p/>
</div>

XML Input:

<root>
    <test name="test1">This is <repl>text</repl> hello</test>
</root>

XSLT Input:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="repl">
    <span>replaced <xsl:value-of select="." /></span>
  </xsl:template>
  <xsl:template match="/">
    <div>
      <h2><xsl:value-of select="test/@name" /></h2>
      <p><xsl:apply-templates select="test/node()" /></p>
    </div>
  </xsl:template>
</xsl:stylesheet>

@jrutila
Copy link
Author

jrutila commented Oct 16, 2024

Hmm.. true. I think I tested this before but you are right. Following xslt works, though.
I had to add match="/root" for some reason.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="repl">
    <span>replaced <xsl:value-of select="." /></span>
  </xsl:template>
  <xsl:template match="/root">
              <div>
                  <h2><xsl:value-of select="test/@name" /></h2>
                  <p><xsl:apply-templates select="test/node()" /></p>
              </div>
  </xsl:template>
</xsl:stylesheet>

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