Search code examples
xmlxsltxslt-2.0

Hashing identity numbers in a XML-file using XSLT-2.0


I have a large XML file containing person data. I need to be able to output the sourcedid/id field hashed, using MD5 or similar.

Since the sourcedid/id field is going to be used as a unique identificator in a DB, the output string must be the same every time this process run for a specific number.

For example, using MD5, then the output from 123456789 will be 25f9e794323b453885f5181f1b624d0b every time.

The XSL conversion will run on a Saxon 2.0 XSL processor. Any ideas?

Example input:

<?xml version="1.0" encoding="UTF-8"?>
<root>    <!-- Added by edit -->
    <person recstatus="1">
        <sourcedid>
            <source>SOFTWARE</source>
            <id>123456789</id>
        </sourcedid>
    </person>
    <person recstatus="1">  
        <sourcedid>
            <source>SOFTWARE</source>
            <id>987654321</id>
        </sourcedid>
    </person>
</root>   <!-- Added by edit -->

Expected output:

<person recstatus="1">
<sourcedid>
<source>SOFTWARE</source>
<id>25f9e794323b453885f5181f1b624d0b</id>
</sourcedid>
</person>
<person recstatus="1">  
<sourcedid>
<source>SOFTWARE</source>
<id>6ebe76c9fb411be97b3b0d48b791a7c9</id>
</sourcedid>
</person>
etc


Solution

  • Use Xalan instead.

    You can just write your java class that does the hashing then you can call it from XSLT using xalan extensions.

    Here is an example:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet
       id="hash-function"
       version="1.0"
       xmlns="urn:com:xslt-hashData"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
       xmlns:java="http://xml.apache.org/xalan/java"
       xmlns:xalan="http://xml.apache.org/xalan"
       xmlns:decryption="xalan://com.eddo.sec.HashClass"
       extension-element-prefixes="java hashit"
    >
    
      <xalan:component prefix="hashit"
                       elements=" " functions="doHash">
        <xalan:script lang="javaclass" src="xalan://com.eddo.sec.HashClass"/>
      </xalan:component>
    
       <xsl:template name="hashData">
          <xsl:param name="inData" />
        <xsl:value-of select="hashit:doHash($inEncData)" />
    
    
       </xsl:template>
    
    </xsl:stylesheet>
    

    Then call the template from your XSLT.

    https://xml.apache.org/xalan-j/extensions.html