Search code examples
base64xslt-2.0xpath-2.0

Write binary file from base64 content


I have base64 encoded contents (image file) and I'd like to write this to an external file using XSLT/XPath 2.0.

This is my input file

<root>
  <img>iVBORw0KGgoAAAANSUhEUgAABAAAAAMAAQMAAACAdIdOAAAABlBMVEUAAAD///
    +l2Z/dAAABpElEQVR42u3OQQ0AMAgEsHOAf7Wbhn0GIa2C5jSLgICAgICAgICAgI
    CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI
    CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMDgQB6UgICAgICAgICAgICAgI
    CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI
    CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI
    CAgICAgICAgICAgICAgICAgICAgICAgMDKwB8CAgICAgICAgICAgICAgICAgICAg
    ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
    ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
    ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAu2BC6XQXOr9fnZDAAAAAElFTkSuQmCC</img>
</root>

And this my my attempt to write the file:

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:file="http://expath.org/ns/file">

  <xsl:template match="img">
    <myimg>
      <xsl:variable name="filename" select="'hello.png'"/>
      <xsl:attribute name="filename" select="$filename"/>
      <xsl:value-of select="file:write-binary($filename,xs:base64Binary(.))" />
    </myimg>
  </xsl:template>
</xsl:stylesheet> 

But "nothing happens", which means I get an XML file with myimg as the root tag (expected), but no file written into the current directory. What am I supposed to do?

I use Saxon-PE-9.7.0.15 with oXygen XML


Edit: use hello.png as a filename (to lessen the confusion)


Solution

  • It seems a problem related to using Saxon and the EXPath file module inside oXygen because when I run the XSLT outside oXygen with Saxon the file is created in the same directory as the XML input and stylesheet code, however inside oXygen the use of <xsl:message select="'current-dir() ', file:current-dir()"/> indicates the file module uses a different directory to read from and write to.