Skip to content

Commit 118ade6

Browse files
Not applying for-each if XPath doesn't find nodes. (#81)
1 parent 67fc6f2 commit 118ade6

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/xslt/xslt.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ export class Xslt {
573573
protected xsltForEach(context: ExprContext, template: XNode, output: XNode) {
574574
const select = xmlGetAttribute(template, 'select');
575575
const nodes = this.xPath.xPathEval(select, context).nodeSetValue();
576+
if (nodes.length === 0) {
577+
return;
578+
}
579+
576580
const sortContext = context.clone(nodes);
577581
this.xsltSort(sortContext, template);
578582

tests/lmht/lmht.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,4 +1601,15 @@ describe('LMHT', () => {
16011601

16021602
assert.equal(outXmlString, expectedOutString);
16031603
});
1604+
1605+
it('Issue 80', () => {
1606+
const xmlString = `<lmht><cabeca><titulo>Listagem de clientes</titulo></cabeca><corpo><titulo1>Clientes</titulo1><tabela><cabeca-tabela><linha><celula>Id</celula><celula>Nome</celula></linha></cabeca-tabela><corpo-tabela><linha><celula>1</celula><celula>Italo</celula></linha><linha><celula>2</celula><celula>Leonel</celula></linha></corpo-tabela></tabela></corpo></lmht>`;
1607+
const xsltClass = new Xslt({ selfClosingTags: false });
1608+
const xmlParser = new XmlParser();
1609+
const xml = xmlParser.xmlParse(xmlString);
1610+
const xslt = xmlParser.xmlParse(xsltString);
1611+
const outXmlString = xsltClass.xsltProcess(xml, xslt);
1612+
1613+
assert.ok(outXmlString);
1614+
});
16041615
});

0 commit comments

Comments
 (0)