58615

Looking for a utility to mask xml content in form of key value tags

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>

Recommend

  • Insert '-' every 5 characters as users type [like a product key] [duplicate]
  • Matplotlib: Plot 3d data with alpha
  • Echo jpassword character once and then hide it
  • Tab files into pandas dataframe according to columns with missing headers
  • ASP.NET MVC Hide Content Directory in url
  • update numpy array where not masked
  • VB.Net Custom Mask for Date/Time AM/PM
  • Implementing IDataErrorInfo using Castle.DynamicProxy in lazy loading scenario using NHibernate
  • How do I retrieve the text in a table column using Selenium RC?
  • Failing to get duration of youtube video using xpath
  • Searching an XML file using PHP [closed]
  • parsing xml and html page with lxml and requests package in python
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • Symfony 2 error page response
  • How do I remove all but some records based on a threshold?
  • Which open source license has no forking [closed]
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • Azure table store snapshot/backup capability
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • How to get current document uri in XSLT?
  • SharedPreferences or SQLite Database?
  • Installed module is empty
  • print() is showing quotation marks in results
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Android fill_parent issue
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • NHibernate Validation Localization with S#arp Architecture
  • Bug in WPF DataGrid
  • Get object from AWS S3 as a stream
  • Redux, normalised entities and lodash merge
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to disable jQuery.jplayer autoplay?
  • Acquiring multiple attributes from .xml file in c#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal