diff --git a/.gitignore b/.gitignore index dfe6864..dcab4b9 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ dist/ node_modules/ package-lock.json +.history/ diff --git a/src/xpath/expressions/function-call-expr.ts b/src/xpath/expressions/function-call-expr.ts index 2690c58..ef5802e 100644 --- a/src/xpath/expressions/function-call-expr.ts +++ b/src/xpath/expressions/function-call-expr.ts @@ -35,7 +35,7 @@ import { formatNumber } from '../functions'; import { extCardinal, extIf, extJoin } from '../functions/non-standard'; -import { lowerCase, upperCase } from '../functions/standard-20'; +import { lowerCase, _replace, upperCase } from '../functions/standard-20'; import { BooleanValue } from '../values/boolean-value'; import { Expression } from './expression'; @@ -60,6 +60,7 @@ export class FunctionCallExpr extends Expression { last, 'local-name': localName, 'lower-case': lowerCase, + 'replace': _replace, matches, name: _name, 'namespace-uri': namespaceUri, diff --git a/src/xpath/functions/standard-20.ts b/src/xpath/functions/standard-20.ts index 717c6fc..8848826 100644 --- a/src/xpath/functions/standard-20.ts +++ b/src/xpath/functions/standard-20.ts @@ -13,3 +13,13 @@ export function lowerCase(context: ExprContext) { const str: string = this.args[0].evaluate(context).stringValue(); return new StringValue(str.toLowerCase()); } + +export function _replace(context: ExprContext) { + assert(['2.0', '3.0'].includes(context.xsltVersion)); + const str: string = this.args[0].evaluate(context).stringValue(); + const s1 = this.args[1].evaluate(context).stringValue(); + const s2 = this.args[2].evaluate(context).stringValue(); + + const searchPattern = new RegExp(s1, 'g'); + return new StringValue(str.replace(searchPattern, s2)); +} diff --git a/tests/xpath/functions.test.tsx b/tests/xpath/functions.test.tsx index 284968c..d55c059 100644 --- a/tests/xpath/functions.test.tsx +++ b/tests/xpath/functions.test.tsx @@ -286,5 +286,51 @@ describe('XPath Functions', () => { assert.equal(outXmlString, 'lily'); }); + + it('replace simple text', () => { + const xmlString = ( + + ); + + const xsltString = ( + + + + + + ) + + const xsltClass = new Xslt(); + + const xml = xmlParser.xmlParse(xmlString); + const xslt = xmlParser.xmlParse(xsltString); + + const outXmlString = xsltClass.xsltProcess(xml, xslt); + + assert.equal(outXmlString, '*ly'); + }); + + it('replace regex text', () => { + const xmlString = ( + + ); + + const xsltString = ( + + + + + + ) + + const xsltClass = new Xslt(); + + const xml = xmlParser.xmlParse(xmlString); + const xslt = xmlParser.xmlParse(xsltString); + + const outXmlString = xsltClass.xsltProcess(xml, xslt); + + assert.equal(outXmlString, '123456789'); + }); }); });