Skip to content
Advertisement

XSLT – masking data – Conditional on other tags

I am trying to mask an xml document where some specific tags are present. I have created a java app which contains saxon9he as dependency.

JavaScript

I have multiple use case, some are straight forward but some are conditional. Assuming the below given <Prsn> tag is present at multiple different locations:

Input xml snippet

JavaScript

Transformation that is needed

In this above provided XML, we have some tags [FrstNm, Nm, BirthDt] which we need to mask (remove the actual data from these tags and replace with # for each character), which by the way I have achieved so far.

Need Help

Tricky part is when we have tag <Othr><SchmeNm><Cd> which can have values [NIND, CCPT, CONCAT], we need to mask <Othr><id>, but any other value in <Othr><SchmeNm><Cd> apart from NIND, CCPT, CONCAT then no change in <Othr><id>.

Transformation.xsl

JavaScript

Advertisement

Answer

If you want to do regex-based search and replace, the minimum XSLT version you need is XSLT 2.0.

Also, don’t use local-name(). Register a prefix for the namespace URI and use that. The prefix does not have to match the XML document, as long as the namespace URI is the same.

Input:

JavaScript

XSLT 2.0+:

JavaScript

Output:

JavaScript

If you only have XSLT 1.0 available, you can use translate(). But that requires that you either explicitly list all possible input characters:

JavaScript

or that you settle on something simpler:

JavaScript

Tricky part is when we have tag <Othr><SchmeNm><Cd> which can have values [NIND, CCPT, CONCAT], we need to mask <Othr><id>, but any other value in <Othr><SchmeNm><Cd> apart from NIND, CCPT, CONCAT then no change in <Othr><id>.

That’s easy. in XSLT 1.0+ this works:

JavaScript

or even this:

JavaScript

In XSLT 2.0+ you can use sequences:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement