
Question:
I have a usecase where I want to mask my xml which has field and value like below:
<root>
<entries>
<entry>
<key>key1</key>
<value>value1</value>
</entry>
<entry>
<key>key2</key>
<value>value2</value>
</entry>
<entry>
<key>key3</key>
<value>value3</value>
</entry>
</entries>
</root>
I want a utility which can does this by providing some configuration where I can just provide the field names that needs to be masked. Writing a full custom code using xpath and than reading the field names explicitly which needs to be masked and than does the masking is not scalable solution considering the addition of fields in the xml in future.
Let me know if there is some utility which can be used for this.
Answer1:You should <a href="https://www.w3schools.com/xml/xsl_intro.asp" rel="nofollow">learn xsl</a>. This is a very powerful mechanism allowing you to process your structured xml data.
I am not an expert in xls but I've prepared you an example of solving your issue with xsl. In my example you can choose which keys to mask and which not. Without filtering it would be even easier:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<root>
<entries>
<xsl:for-each select="//value">
<xsl:variable name="key" select="./parent::node()/key"/>
<entry>
<key>
<xsl:value-of select="$key"/>
</key>
<value>
<xsl:choose>
<xsl:when test="$key='key1'">###</xsl:when>
<xsl:when test="$key='key3'">###</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</value>
</entry>
</xsl:for-each>
</entries>
</root>
</xsl:template>
</xsl:stylesheet>
So the output of such transformation will be:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<entries>
<entry>
<key>key1</key>
<value>###</value>
</entry>
<entry>
<key>key2</key>
<value>value2</value>
</entry>
<entry>
<key>key3</key>
<value>###</value>
</entry>
</entries>
</root>
To run this you need to have xsl processor: <a href="https://stackoverflow.com/questions/1590085/how-do-i-run-an-xslt-file" rel="nofollow">How do I run an XSLT file?</a>
I have tested my solution in <a href="http://xslttest.appspot.com/" rel="nofollow">online xsl test tool.</a>