Skip to content
Advertisement

XSLT filitering for both begin with and followed by characters

I am working in project where I am given a list of allowed characters, and required to remove the unwanted characters. I have the following done, but I felt it is cumbersome and than it should be

JavaScript

Testing the start condition I have added three checks. The contains check returns true for empty string case, and thus I have added string length condition in order to return a NULL for empty string case.

JavaScript

My XML for testing is below

JavaScript

I may have missed any edge cases, my question is is there a better way to solving this XSLT filtering task where starting and consecutive characters are conditional?

EDIT Reading michael.hor257k comment made me question my approach and understand my requirement more. There is Cybersource page where it specifies allowed characters when making requests to their api. My target is to clean unwanted characters and make sure that field begin and followed by characters meets the specs given on the website. Take Ship-To Company name as example. I am using XSLT 1.0 with java Transformer class

Advertisement

Answer

Consider the following simplified example:

XML

JavaScript

XSLT 1.0

JavaScript

Result

JavaScript

You might want to add a test for the case where all characters turn out to be illegal – although that seems highly unlikely.


Added:

If all you want is to test the input for being valid, then you could do:

JavaScript

to get:

JavaScript
Advertisement