Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hash a field in XML using XSLT

Tags:

xml

xslt

There is a XML from that I am constructing an another XML using XSLT. I want some fields to have Hashed Value rather than actual value. Meaning I should know when data is changed but I don't want to know the data due to some security reasons.

<xsl:template name="sensitiveDataTemplate">
    <xsl:param name="sensitiveData"></xsl:param>
    <xsl:if test="$sensitiveData!=''">
        <xsl:value-of select="'XXXXXX'"></xsl:value-of>
    </xsl:if>
</xsl:template>

For now I am just replacing data with XXXXX but I need some hashed value here.

like image 370
Saurabh Prajapati Avatar asked Mar 05 '18 06:03

Saurabh Prajapati


1 Answers

For generating the hash value you may register the custom functions.

Refer to the official documentation on how to register custom php function in xlst processor.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl">
 <xsl:template name="sensitiveDataTemplate">
    <xsl:param name="sensitiveData"></xsl:param>
    <xsl:if test="$sensitiveData!=''">
        <xsl:value-of select="'php:some_hash_fun()'"></xsl:value-of>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>

function some_hash_fun( )
{
    return "XXXX"; // hash value
}
$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);

$proc = new XSLTProcessor();
$proc->registerPHPFunctions();  // can be either a string (a function name) or an array of functions.
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);

Hope it helps.

like image 60
skadya Avatar answered Sep 28 '22 06:09

skadya