1

Is there any way on how to convert a string to binary base64? I've seen many references but it didn't work in my end. For example I have this input file:

<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <Data>
         <Binary>
               <RawData>This element should convert string to binary base64.</RawData>
         </Binary>
    </Data>
</RootElement>

I need to generate:

<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Data>
    <Binary>
        <RawData>VGhpcyBlbGVtZW50IHNob3VsZCBjb252ZXJ0IHN0cmluZyB0byBiaW5hcnkgYmFzZTY0Lg==</RawData>
    </Binary>
</Data>

I created an xslt and used the namespace I've seen online:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dp="http://www.datapower.com/extensions">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="RawData">
    <xsl:element name="RawData">
        <xsl:value-of select="dp:encode(., 'base-64')"/>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

Thank you.

pinkpanther
  • 157
  • 3
  • 11
  • 2
    Which XSLT processor? Just including the namespace doesn't make the functions available. You must install the libraries that implement the namespace as well. You have not provided enough information for anyone to answer. – Jim Garrison Jan 20 '17 at 06:43
  • It would help if you could tell us the XSLT processor and XSLT version you are using. Microsoft's MSXSL extension gives you a `` element which you can use to define custom functions in JScript/C#/etc. to do transformations on your data. – Madeyedexter Jan 20 '17 at 06:44
  • 2
    If you're using the Saxon XSLT processor, you can use a specific function, see http://www.saxonica.com/html/documentation/functions/saxon/string-to-base64Binary.html. – potame Jan 20 '17 at 07:55
  • I don't think you can use IBM Datapower's extension functions somewhere else in another processor, they are specific to the datapower boxes. – Stefan Hegny Jan 20 '17 at 10:50
  • 1
    See also http://www.saxonica.com/html/documentation/functions/expath-binary/encode-string.html for Saxon 9.6 PE/EE and later. – Martin Honnen Jan 20 '17 at 11:58

1 Answers1

6

There is a pure XSLT 1.0 solution that works for any XSLT processor: JAXP, Saxon, Xalan, Xsltproc, Microsoft:

  1. Download base64.xsl
  2. Download base64_binarydatamap.xml
  3. Use XSLT 1.0:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b64="https://github.com/ilyakharlamov/xslt_base64">
        <xsl:output method="xml"/>
        <xsl:include href="base64.xsl"/>
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:template>
        <xsl:template match="/RootElement/Data/Binary/RawData">
            <xsl:call-template name="b64:encode">
                <xsl:with-param name="asciiString" select="text()"/>
            </xsl:call-template>
        </xsl:template>
    </xsl:stylesheet>
    
Ilya Kharlamov
  • 3,698
  • 1
  • 31
  • 33