Search code examples
xslttype-conversionhexlong-integeribm-datapower

How to convert Long datatype to Hex format in XSLT and Perform Long Parsing in XSLT. XSLT to be used in DataPower


Below is my .Net VB code. How do we implement below code in XSLT (XSLT to be used in DataPower). Since we do not have Long datatype in XSLT, we're having tough time to implement below .Net code in XSLT. Can you please help.

Public Sub Main(args() As string)
  'Inputs are hardcoded
  Dim strLength As Integer = 8
  Dim longValue As Long = 2

  'We're trying to convert Long(819) to Hex String of Length 8
  Dim hexString As String = longValue.ToString("X" & strLength)

  'Declaring Variables
  Dim intLoop As Integer
  Dim longTempValue As Long
  Dim stringBuffer As New System.Text.StringBuilder(strLength)

  For intLoop = 0 To strLength -2
  longTempValue = Long.Parse(hexString.Substring(intLoop,2), Globalization.NumberStyles.AllowHexSpecifier)
  stringBuffer.Append(Chr(longTempValue))
  intLoop+=2
  Next

  Console.WriteLine(stringBuffer.ToString())
End Sub

But Input is a Long datatype but since we considered decimal input which couldnt handle input value &H111 due to range restriction. I've checked online compiler and could get &H111 hex value as 00000111

Now lets consider hex value as input(00000111) and take 2 characters at a time
(1)Convert string representation of a number in specified style to its 64bit signed integer equivalent
(2)Convert to Character Code(represents particular character in a set, such as ANSI character set)

How do we handle these conversions in XSLT? I've updated complete VB code above


Solution

  • Here is an example showing how you can convert a decimal value to hexadecimal notation with fixed 8 digits:

    XML

    <input>
        <decimal>0</decimal>
        <decimal>1</decimal>
        <decimal>9</decimal>
        <decimal>10</decimal>
        <decimal>15</decimal>
        <decimal>16</decimal>
        <decimal>34</decimal>
        <decimal>96</decimal>
        <decimal>178</decimal>
        <decimal>506</decimal>
        <decimal>1308</decimal>
        <decimal>9734</decimal>
        <decimal>53789</decimal>
        <decimal>786120</decimal>
    </input>
    

    XSLT 2.0

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="math">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/input">
        <output>
            <xsl:for-each select="decimal">
                <xsl:variable name="hex-values" select="for $i in 0 to 7 return . idiv math:pow(16, $i) mod 16"/>    
                <hex>
                    <xsl:value-of select="for $j in reverse($hex-values) return codepoints-to-string($j + (if($j > 9) then 55 else 48))" separator=""/>
                </hex>
            </xsl:for-each>
        </output>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Result

    <?xml version="1.0" encoding="UTF-8"?>
    <output>
       <hex>00000000</hex>
       <hex>00000001</hex>
       <hex>00000009</hex>
       <hex>0000000A</hex>
       <hex>0000000F</hex>
       <hex>00000010</hex>
       <hex>00000022</hex>
       <hex>00000060</hex>
       <hex>000000B2</hex>
       <hex>000001FA</hex>
       <hex>0000051C</hex>
       <hex>00002606</hex>
       <hex>0000D21D</hex>
       <hex>000BFEC8</hex>
    </output>
    

    Naturally, with an output of fixed 8 hexadecimal digits, the input values are expected to be integers in the range of 0 to 4294967295.