XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Cynthia van BovenSubject: Base64 decoder
Author: Cynthia van Boven
Date: 16 Sep 2009 03:47 AM
Good morning from the Netherlands,

I am now perfectly able to encode files to base64 format and upload them to an SQL application. This application is missing however an export functionality, so I am now trying to get the data from the database with an xquery and transform the base64 encoded field back to binary format.

I found this xsl sheet on the Internet, should I able to do the trick with this sheet? For now I am not able to run it due to a "no character data is allowed between top level elements", but I don't know what's the problem and searching on the Stylus form returns a system error since yesterday.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="datamap" select="document('datamap.xml')" />
<xsl:template match="/">
<xsl:variable name="base64String1" select="'SGVsbG8gV29ybGQh'" />
<xsl:variable name="base64String2" select="'VGhpcyBpcyBiYXNlNjQgZW5jb2Rpbmc='" />
<xsl:variable name="base64String3" select="'Ymx1ZQ=='" />
<xsl:call-template name="convertBase64ToAscii">
<xsl:with-param name="base64String" select="$base64String1" />
</xsl:call-template>
<xsl:text>------------------------</xsl:text>
<xsl:call-template name="convertBase64ToAscii">
<xsl:with-param name="base64String" select="$base64String2" />
</xsl:call-template>
<xsl:text>------------------------</xsl:text>
<xsl:call-template name="convertBase64ToAscii">
<xsl:with-param name="base64String" select="$base64String3" />
</xsl:call-template>
<xsl:text>------------------------</xsl:text>
</xsl:template>
<!-- Template to convert the base64 string to ascii representation
-->
<xsl:template name="convertBase64ToAscii">
<xsl:param name="base64String" />
<!-- execute if last 2 characters do not contain = character
-->
<xsl:if test="not(contains(substring($base64String, string-length($base64String) - 1), '='))">
<xsl:variable name="binaryBase64String">
<xsl:call-template name="base64StringToBinary">
<xsl:with-param name="string" select="$base64String" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="base64BinaryStringToAscii">
<xsl:with-param name="binaryString" select="$binaryBase64String" />
</xsl:call-template>
</xsl:if>
<!-- extract last two characters
-->
<xsl:variable name="secondLastChar" select="substring($base64String, string-length($base64String) - 1, 1)" />
<xsl:variable name="lastChar" select="substring($base64String, string-length($base64String), 1)" /> <!-- execute if 2nd last character is not a =, and last character is =
-->
<xsl:if test="($secondLastChar != '=') and ($lastChar = '=')">
<xsl:variable name="binaryBase64String">
<xsl:call-template name="base64StringToBinary">
<xsl:with-param name="string" select="substring($base64String, 1, string-length($base64String) - 4)" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="base64BinaryStringToAscii">
<xsl:with-param name="binaryString" select="$binaryBase64String" />
</xsl:call-template>
<xsl:variable name="partialBinary">
<xsl:call-template name="base64StringToBinary">
<xsl:with-param name="string" select="substring($base64String, string-length($base64String) - 3, 3)" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="base64BinaryStringToAscii">
<xsl:with-param name="binaryString" select="substring($partialBinary, 1, 8)" />
</xsl:call-template>
<xsl:call-template name="base64BinaryStringToAscii">
<xsl:with-param name="binaryString" select="substring($partialBinary, 9, 8)" />
</xsl:call-template>
</xsl:if>
<!-- execute if last 2 characters are both =
-->
<xsl:if test="($secondLastChar = '=') and ($lastChar = '=')">
<xsl:variable name="binaryBase64String">
<xsl:call-template name="base64StringToBinary">
<xsl:with-param name="string" select="substring($base64String, 1, string-length($base64String) - 4)" />
</xsl:call-template>
</xsl:variable>
- <xsl:call-template name="base64BinaryStringToAscii">
<xsl:with-param name="binaryString" select="$binaryBase64String" />
</xsl:call-template>
<xsl:variable name="partialBinary">
<xsl:call-template name="base64StringToBinary">
<xsl:with-param name="string" select="substring($base64String, string-length($base64String) - 3, 2)" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="base64BinaryStringToAscii">
<xsl:with-param name="binaryString" select="substring($partialBinary, 1, 8)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Template to convert the base64 binary string to ascii representation
-->
<xsl:template name="base64BinaryStringToAscii">
<xsl:param name="binaryString" />
<xsl:if test="substring($binaryString, 1, 8) != ''">
<xsl:variable name="asciiDecimal">
<xsl:call-template name="binaryToDecimal">
<xsl:with-param name="binary" select="substring($binaryString, 1, 8)" />
<xsl:with-param name="sum" select="0" />
<xsl:with-param name="index" select="0" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$datamap/datamap/asciidecimal/char[decimal = $asciiDecimal]/ascii" />
<xsl:call-template name="base64BinaryStringToAscii">
<xsl:with-param name="binaryString" select="substring($binaryString, 9)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Template to convert a binary number to decimal representation; this template calls template pow
-->
<xsl:template name="binaryToDecimal">
<xsl:param name="binary" />
<xsl:param name="sum" />
<xsl:param name="index" />
<xsl:if test="substring($binary,string-length($binary) - 1) != ''">
<xsl:variable name="power">
<xsl:call-template name="pow">
<xsl:with-param name="m" select="2" />
<xsl:with-param name="n" select="$index" />
<xsl:with-param name="result" select="1" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="binaryToDecimal">
<xsl:with-param name="binary" select="substring($binary, 1, string-length($binary) - 1)" />
<xsl:with-param name="sum" select="$sum + substring($binary,string-length($binary)) * $power" />
<xsl:with-param name="index" select="$index + 1" />
</xsl:call-template>
</xsl:if>
<xsl:if test="substring($binary,string-length($binary) - 1) = ''">
<xsl:value-of select="$sum" />
</xsl:if>
</xsl:template>
<!-- Template to calculate m to the power n
-->
<xsl:template name="pow">
<xsl:param name="m" />
<xsl:param name="n" />
<xsl:param name="result" />
<xsl:if test="$n >= 1">
<xsl:call-template name="pow">
<xsl:with-param name="m" select="$m" />
<xsl:with-param name="n" select="$n - 1" />
<xsl:with-param name="result" select="$result * $m" />
</xsl:call-template>
</xsl:if>
<xsl:if test="$n = 0">
<xsl:value-of select="$result" />
</xsl:if>
</xsl:template>
<!-- Template to convert a base64 string to binary representation; this template calls template decimalToBinary
-->
<xsl:template name="base64StringToBinary">
<xsl:param name="string" />
<xsl:if test="substring($string, 1, 1) != ''">
<xsl:variable name="binary">
<xsl:call-template name="decimalToBinary">
<xsl:with-param name="decimal" select="$datamap/datamap/decimalbase64/char[base64 = substring($string, 1, 1)]/value" />
<xsl:with-param name="prev" select="''" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="padZeros">
<xsl:with-param name="string" select="$binary" />
<xsl:with-param name="no" select="6 - string-length($binary)" />
</xsl:call-template>
</xsl:if>
<xsl:if test="substring($string, 2) != ''">
<xsl:call-template name="base64StringToBinary">
<xsl:with-param name="string" select="substring($string, 2)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Template to left pad a binary string, with the specified no of 0s, to make it of length 6
-->
- <xsl:template name="padZeros">
<xsl:param name="string" />
<xsl:param name="no" />
<xsl:if test="$no > 0">
<xsl:call-template name="padZeros">
<xsl:with-param name="string" select="concat('0', $string)" />
<xsl:with-param name="no" select="6 - string-length($string) - 1" />
</xsl:call-template>
</xsl:if>
<xsl:if test="$no = 0">
<xsl:value-of select="$string" />
</xsl:if>
</xsl:template>
<!-- Template to convert a decimal number to binary representation
-->
<xsl:template name="decimalToBinary">
<xsl:param name="decimal" />
<xsl:param name="prev" />
<xsl:variable name="divresult" select="floor($decimal div 2)" />
<xsl:variable name="modresult" select="$decimal mod 2" />
<xsl:choose>
<xsl:when test="$divresult > 1">
<xsl:call-template name="decimalToBinary">
<xsl:with-param name="decimal" select="$divresult" />
<xsl:with-param name="prev" select="concat($modresult, $prev)" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$divresult = 0">
<xsl:value-of select="concat($modresult, $prev)" />
</xsl:when>
<xsl:when test="$divresult = 1">
<xsl:text>1</xsl:text>
<xsl:value-of select="concat($modresult, $prev)" />
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Thanks again,


Cynthia

Postnext
Cynthia van BovenSubject: Base64 decoder - easier solution?
Author: Cynthia van Boven
Date: 16 Sep 2009 09:54 AM
Dear Stylussees,

I think I found a far more easy solution on your own website, see http://www.stylusstudio.com/binary_xml.html. I tested the emaple on your own website, but it is not working. Could it be that it's not working, because I don't have the Base-64 Deployment Adapter? I have a full license of Stylus Studio Enterprise.

Thanks,



Cynthia

Postnext
Tony LavinioSubject: Base64 decoder - easier solution?
Author: Tony Lavinio
Date: 17 Sep 2009 01:52 AM
If you are running from within Stylus Studio, you have a development-time
license to the converters.

First, were you able to get the examples from the website working?
(note that the 'adapter:' prefix is now 'converter:', but both should
work).

Postnext
Cynthia van BovenSubject: Base64 decoder - easier solution?
Author: Cynthia van Boven
Date: 18 Sep 2009 03:01 PM
I got the example working, but only after I installed the trial XML Converter - Java (or flat file converter). I thought it would be like you said, I have a Stylus Enterprise license, so I should be able to use the converters from within Stylus.

Ever since I tried to compile a Java coded XSLT Stylus is telling me though this license is past its evaluation period.

How can I run the base64 decoding XSLT without an extra license from within Stylus?

Thanks again,


Cynthia

Posttop
Tony LavinioSubject: Base64 decoder - easier solution?
Author: Tony Lavinio
Date: 18 Sep 2009 09:41 PM
You were getting a license error from within Stylus Studio for the
converters?

Please send a screenshot showing that, along with your license number
to stylus-field-report@progress.com, and we'll look to see what has
happened.

Is it possible you have a mismatch between the Stylus Studio version
and a separately-installed XML Converters license?

Within Enterprise Stylus Studio, you should have full access to the
XML Converters and XQuery engine.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.