Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/xslt/xslt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1332,10 +1332,13 @@

// node.transformedNodeName = name;

// Fix for Issue 161: Set siblingPosition to preserve document order
node.siblingPosition = (output || this.outputDocument).childNodes.length;

Check warning on line 1336 in src/xslt/xslt.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

domAppendChild(output || this.outputDocument, node);
// The element becomes the output node of the source node.
// context.nodeList[context.position].outputNode = node;
const clonedContext = context.clone(undefined, 0);
const clonedContext = context.clone();
await this.xsltChildNodes(clonedContext, template, node);
}

Expand Down
74 changes: 74 additions & 0 deletions tests/gh_issues_160_161.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable no-undef */
import assert from 'assert';

import { Xslt } from '../src/xslt';
import { XmlParser } from '../src/dom';

describe('Issues 160 and 161', () => {

// Issue 160
it('Issue 160: xsl:element inside for-each should use correct context node', async () => {
const xmlString = `
<data>
<item id="1">Item 1</item>
<item id="2">Item 2</item>
</data>`;

const xsltString = `
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<root>
<xsl:for-each select="data/item">
<xsl:element name="row">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>`;

const expectedOutString = `<root><row>Item 1</row><row>Item 2</row></root>`;

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);

// Normalize whitespace for comparison if needed, but for now exact match
assert.equal(outXmlString.replace(/\s+/g, ''), expectedOutString.replace(/\s+/g, ''));
});

// Issue 161
it('Issue 161: Mixing xsl:element and literal tags should preserve order', async () => {
const xmlString = `
<data>
<item id="1">Item 1</item>
<item id="2">Item 2</item>
</data>`;

const xsltString = `
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<root>
<xsl:for-each select="data/item">
<xsl:element name="generated">Gen <xsl:value-of select="@id"/></xsl:element>
<literal>Lit <xsl:value-of select="@id"/></literal>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>`;

// Expected: Interleaved
const expectedOutString = `<root><generated>Gen 1</generated><literal>Lit 1</literal><generated>Gen 2</generated><literal>Lit 2</literal></root>`;

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);

// console.log('Issue 161 Output:', outXmlString);
assert.equal(outXmlString.replace(/\s+/g, ''), expectedOutString.replace(/\s+/g, ''));
});
});