Skip to content

Commit f873a59

Browse files
authored
implement replace function (#87)
1 parent 254be3f commit f873a59

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dist/
66
node_modules/
77

88
package-lock.json
9+
.history/

src/xpath/expressions/function-call-expr.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
formatNumber
3636
} from '../functions';
3737
import { extCardinal, extIf, extJoin } from '../functions/non-standard';
38-
import { lowerCase, upperCase } from '../functions/standard-20';
38+
import { lowerCase, _replace, upperCase } from '../functions/standard-20';
3939
import { BooleanValue } from '../values/boolean-value';
4040
import { Expression } from './expression';
4141

@@ -60,6 +60,7 @@ export class FunctionCallExpr extends Expression {
6060
last,
6161
'local-name': localName,
6262
'lower-case': lowerCase,
63+
'replace': _replace,
6364
matches,
6465
name: _name,
6566
'namespace-uri': namespaceUri,

src/xpath/functions/standard-20.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ export function lowerCase(context: ExprContext) {
1313
const str: string = this.args[0].evaluate(context).stringValue();
1414
return new StringValue(str.toLowerCase());
1515
}
16+
17+
export function _replace(context: ExprContext) {
18+
assert(['2.0', '3.0'].includes(context.xsltVersion));
19+
const str: string = this.args[0].evaluate(context).stringValue();
20+
const s1 = this.args[1].evaluate(context).stringValue();
21+
const s2 = this.args[2].evaluate(context).stringValue();
22+
23+
const searchPattern = new RegExp(s1, 'g');
24+
return new StringValue(str.replace(searchPattern, s2));
25+
}

tests/xpath/functions.test.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,51 @@ describe('XPath Functions', () => {
286286

287287
assert.equal(outXmlString, 'lily');
288288
});
289+
290+
it('replace simple text', () => {
291+
const xmlString = (
292+
<root></root>
293+
);
294+
295+
const xsltString = (
296+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
297+
<xsl:template match="/">
298+
<xsl:value-of select="replace('Lily','Li','*')" />
299+
</xsl:template>
300+
</xsl:stylesheet>
301+
)
302+
303+
const xsltClass = new Xslt();
304+
305+
const xml = xmlParser.xmlParse(xmlString);
306+
const xslt = xmlParser.xmlParse(xsltString);
307+
308+
const outXmlString = xsltClass.xsltProcess(xml, xslt);
309+
310+
assert.equal(outXmlString, '*ly');
311+
});
312+
313+
it('replace regex text', () => {
314+
const xmlString = (
315+
<root></root>
316+
);
317+
318+
const xsltString = (
319+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
320+
<xsl:template match="/">
321+
<xsl:value-of select="replace('This is some 123 text 456 with 789 numbers.', '[^\d]+', '')" />
322+
</xsl:template>
323+
</xsl:stylesheet>
324+
)
325+
326+
const xsltClass = new Xslt();
327+
328+
const xml = xmlParser.xmlParse(xmlString);
329+
const xslt = xmlParser.xmlParse(xsltString);
330+
331+
const outXmlString = xsltClass.xsltProcess(xml, xslt);
332+
333+
assert.equal(outXmlString, '123456789');
334+
});
289335
});
290336
});

0 commit comments

Comments
 (0)