Re: [xsl] XSLT - Converting image to Base64

Subject: Re: [xsl] XSLT - Converting image to Base64
From: Albert Juhé <albertjuhe@xxxxxxxxx>
Date: Wed, 27 Apr 2011 13:12:30 +0200
Hi tom,

I have this xsl to convert base64 to image file, I use Saxon and java.
I use this template for transforming xhtml word to docbook, sometimes
the image files are coded in base64, I extract the content and save
the file with generate-id name, after this I put the reference in the
docbook file.

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:xhtml="http://www.w3.org/1999/xhtml";
    xmlns="http://docbook.org/ns/docbook";
xmlns:b64="net.sf.saxon.value.Base64BinaryValue"
    xmlns:fos="java.io.FileOutputStream"
xpath-default-namespace="http://www.w3.org/1999/xhtml";
    exclude-result-prefixes="#all">


    <xsl:template match="img">
        <xsl:variable name="contingut"
select="substring-after(@src,'data:image/*;base64,')"/>
        <xsl:variable name="img" select="concat(generate-id(),'.jpg')"/>
        <xsl:variable name="b64" select="b64:new(string($contingut))"/>
        <xsl:variable name="fos"
select="fos:new(concat($dir-sortida,'/',string($img)))"/>
        <xsl:value-of select="fos:write($fos, b64:getBinaryValue($b64))"/>
        <xsl:value-of select="fos:close($fos)"/>
        <para>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="{$img}" format="img"/>
                </imageobject>
            </mediaobject>
        </para>
    </xsl:template>


</xsl:stylesheet>

2011/4/27 Martin Honnen <Martin.Honnen@xxxxxx>:
> tom tom wrote:
>
>> I need to take a jpeg / gif file as input and output it as base 64.
>>
>> Is there a way to do this using XSLT 2 (Saxon 9+) / extension functions.
>
> There is
>
http://www.saxonica.com/documentation/extensions/functions/octetstobase64bina
ry.xml
> and by calling into Java you can probably read in the file and get the
> sequence of octets to feed to that function.
>
> --
>
>        Martin Honnen
>        http://msmvps.com/blogs/martin_honnen/

Current Thread