Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I obscure my XML before posting?

XSLT and XML

How can I obscure my XML before posting?

by  k5tm  Posted    (Edited  )
One problem some face when posting to the XML forum is the need to maintain the privacy of proprietary data contained in an XML document. So, the poster will often try to paraphrase the XML document that is at issue and in the process mis-state the structure in a way that leads to miscommunication.

The following XSLT does a reasonable job of obscuring both the XML element and attribute names, and the data, in an XML document.

[code XSLT][small]<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput encoding="utf-8"/>
<xsl:variable name="myTranslate" select="'[highlight #8AE234]ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[/highlight]'"/>

<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="*">
<xsl:variable name="myName"><xsl:call-template name="obfuscate">
<xsl:with-param name="theString" select="local-name()"/>
</xsl:call-template></xsl:variable>
<xsl:element name="{$myName}" namespace="{namespace-uri()}">
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="* | text() | comment()"/>
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:variable name="myName"><xsl:call-template name="obfuscate">
<xsl:with-param name="theString" select="local-name()"/>
</xsl:call-template></xsl:variable>
<xsl:attribute name="{$myName}" namespace="{namespace-uri()}"><xsl:call-template name="obfuscate">
<xsl:with-param name="theString" select="."/>
</xsl:call-template></xsl:attribute>
</xsl:template>

<xsl:template match="text()"><xsl:call-template name="obfuscate">
<xsl:with-param name="theString" select="."/>
</xsl:call-template></xsl:template>

<xsl:template name="obfuscate">
<xsl:param name="theString"/>
<xsl:param name="theTranslate" select="$myTranslate"/>
<xsl:param name="stringSoFar" select="''"/>
<xsl:choose>
<xsl:when test="string-length($theString)=0"><xsl:value-of select="$stringSoFar"/></xsl:when>
<xsl:eek:therwise><xsl:call-template name="obfuscate">
<xsl:with-param name="theString" select="substring($theString,2)"/>
<xsl:with-param name="theTranslate" select="concat(substring($theTranslate,2,51),substring($theTranslate,1,1),substring($theTranslate,54,9),substring($theTranslate,53,1))"/>
<xsl:with-param name="stringSoFar" select="concat($stringSoFar,translate(substring($theString,1,1),'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$theTranslate))"/>
</xsl:call-template>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>[/small][/code]

This works well on Saxon, and reasonably well on MSXML.

You can change the [highlight #8AE234]string defined for myTranslate[/highlight] as you see fit. The obfuscate template shifts the upper case letters, lower case letters and numbers each as a group for each level of recursion. Therefore you might want to group your upper case, lower case and numbers together as in the example. Especially for the numbers, keeping the numbers together allows the obfuscator to produce number strings where there were numbers in the original.

Namespaces are not obfuscated, so this will be something you must do yourself if necessary. (There is no way to match namespace nodes in a template in XSLT/XPath version 1, so obfuscating namespace values would have introduced more complexity for very little benefit.)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top