Skip to content

Commit ac5b151

Browse files
Using variables set in the context to resolve values for call-template. (#83)
1 parent 091318c commit ac5b151

File tree

2 files changed

+1609
-1558
lines changed

2 files changed

+1609
-1558
lines changed

src/xslt/xslt.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,13 @@ export class Xslt {
291291
this.xsltWithParam(paramContext, template);
292292

293293
for (let i = 0; i < top.childNodes.length; ++i) {
294-
let c = top.childNodes[i];
294+
let childNode = top.childNodes[i];
295295
if (
296-
c.nodeType == DOM_ELEMENT_NODE &&
297-
this.isXsltElement(c, 'template') &&
298-
domGetAttributeValue(c, 'name') == name
296+
childNode.nodeType === DOM_ELEMENT_NODE &&
297+
this.isXsltElement(childNode, 'template') &&
298+
domGetAttributeValue(childNode, 'name') == name
299299
) {
300-
this.xsltChildNodes(paramContext, c, output);
300+
this.xsltChildNodes(paramContext, childNode, output);
301301
break;
302302
}
303303
}
@@ -478,26 +478,26 @@ export class Xslt {
478478
}
479479

480480
/**
481-
* Implements xsl:choose and its child nodes xsl:when and
482-
* xsl:otherwise.
483-
* @param input The Expression Context.
481+
* Implements `xsl:choose`, its child nodes `xsl:when`, and
482+
* `xsl:otherwise`.
483+
* @param context The Expression Context.
484484
* @param template The template.
485485
* @param output The output.
486486
*/
487-
protected xsltChoose(input: ExprContext, template: any, output: any) {
487+
protected xsltChoose(context: ExprContext, template: XNode, output: any) {
488488
for (const childNode of template.childNodes) {
489489
if (childNode.nodeType !== DOM_ELEMENT_NODE) {
490490
continue;
491491
}
492492

493493
if (this.isXsltElement(childNode, 'when')) {
494494
const test = xmlGetAttribute(childNode, 'test');
495-
if (this.xPath.xPathEval(test, input).booleanValue()) {
496-
this.xsltChildNodes(input, childNode, output);
495+
if (this.xPath.xPathEval(test, context).booleanValue()) {
496+
this.xsltChildNodes(context, childNode, output);
497497
break;
498498
}
499499
} else if (this.isXsltElement(childNode, 'otherwise')) {
500-
this.xsltChildNodes(input, childNode, output);
500+
this.xsltChildNodes(context, childNode, output);
501501
break;
502502
}
503503
}
@@ -662,25 +662,27 @@ export class Xslt {
662662
* Evaluates a variable or parameter and set it in the current input
663663
* context. Implements `xsl:variable`, `xsl:param`, and `xsl:with-param`.
664664
*
665-
* @param input TODO
666-
* @param template TODO
665+
* @param context The expression context.
666+
* @param template The template node.
667667
* @param override flag that defines if the value computed here
668668
* overrides the one already in the input context if that is the
669669
* case. I.e. decides if this is a default value or a local
670670
* value. `xsl:variable` and `xsl:with-param` override; `xsl:param` doesn't.
671671
*/
672-
protected xsltVariable(input: ExprContext, template: any, override: boolean) {
672+
protected xsltVariable(context: ExprContext, template: XNode, override: boolean) {
673673
const name = xmlGetAttribute(template, 'name');
674674
const select = xmlGetAttribute(template, 'select');
675675

676676
let value: any;
677677

678678
if (template.childNodes.length > 0) {
679679
const root = domCreateDocumentFragment(template.ownerDocument);
680-
this.xsltChildNodes(input, template, root);
680+
this.xsltChildNodes(context, template, root);
681681
value = new NodeSetValue([root]);
682682
} else if (select) {
683-
value = this.xPath.xPathEval(select, input);
683+
value = this.xPath.xPathEval(select, context);
684+
} else if (name in context.variables) {
685+
value = context.variables[name];
684686
} else {
685687
let parameterValue = '';
686688
const filteredParameter = this.options.parameters.filter((p) => p.name === name);
@@ -690,8 +692,8 @@ export class Xslt {
690692
value = new StringValue(parameterValue);
691693
}
692694

693-
if (override || !input.getVariable(name)) {
694-
input.setVariable(name, value);
695+
if (override || !context.getVariable(name)) {
696+
context.setVariable(name, value);
695697
}
696698
}
697699

@@ -892,13 +894,13 @@ export class Xslt {
892894
* current template node, in the current input context. This happens
893895
* before the operation specified by the current template node is
894896
* executed.
895-
* @param input TODO
896-
* @param template TODO
897+
* @param context The Expression Context.
898+
* @param template The template node.
897899
*/
898-
protected xsltWithParam(input: ExprContext, template: any) {
899-
for (const c of template.childNodes) {
900-
if (c.nodeType === DOM_ELEMENT_NODE && this.isXsltElement(c, 'with-param')) {
901-
this.xsltVariable(input, c, true);
900+
protected xsltWithParam(context: ExprContext, template: XNode) {
901+
for (const childNode of template.childNodes) {
902+
if (childNode.nodeType === DOM_ELEMENT_NODE && this.isXsltElement(childNode, 'with-param')) {
903+
this.xsltVariable(context, childNode, true);
902904
}
903905
}
904906
}

0 commit comments

Comments
 (0)