title: Writing Base64 Images in an XML to a File
body: <p>I have a XML which contains Base64 data, what's the best way to strip it out using a XSLT file?  I am using Saxon to reformat the XML and would want to include a filepath in the formatted XML.  I looked at a few examples on SO, but can't seem to get it working.  </p>
  
  <p>I'm trying to extract the Base64 encoded data and write it to an external file, then programatically write it's location in the XML I output.  I may have multiple elements of the same name that contain base64 image data.  I have some code below based on some examples I found online and help from experts, but it returns errors, here's a screenshot of the errors I get. <a href="http://i.imgur.com/5Np9bNp.png" rel="nofollow">http://i.imgur.com/5Np9bNp.png</a></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ProcessArea&gt;
  &lt;DataArea&gt;
          &lt;Process confirm="Always" acknowledge="Never"/&gt;
          &lt;Image&gt;
  &lt;Detail&gt;
                  &lt;TypeId&gt;1&lt;/TypeId&gt;
                  &lt;Description&gt;Description&lt;/Description&gt;
                  &lt;ImageId&gt;525225&lt;/ImageId&gt;
                  &lt;New&gt;true&lt;/New&gt;
                  &lt;PageCount&gt;2&lt;/PageCount&gt;
                  &lt;ImageData&gt;&lt;![CDATA[BASE64String==]]&gt;&lt;/ImageData&gt;
              &lt;/Detail&gt;
          &lt;/Image&gt;
      &lt;/DataArea&gt;
  &lt;/ProcessImage&gt;
  </code></pre>
  
  <p>Below is the XSLT File I am using.</p>
  
  <pre><code>   &lt;!-- Begin XST Style Sheet --&gt;
  &lt;xsl:stylesheet 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:saxon="http://saxon.sf.net/" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="saxon" 
    xmlns:fos="java.io.FileOutputStream"&gt;
  
    &lt;!-- Indent in the output --&gt;
    &lt;xsl:output indent="yes"/&gt;
  
    &lt;!-- Match the root --&gt;
    &lt;xsl:template match="ProcessImage"&gt;
      &lt;Import&gt; &lt;!-- Insert our Import Node --&gt;
        &lt;Archive ConnectionID="1" Name="1"&gt; &lt;!-- Define our Application Connection String --&gt;
          &lt;Document pass="True"&gt; &lt;!-- Statically set documents to pass --&gt;
            &lt;xsl:for-each select="Pages/Page"&gt; &lt;!-- Ignore --&gt;
              &lt;DocFile FileLoc="{@FileName}" /&gt; &lt;!-- Ignore --&gt;
            &lt;/xsl:for-each&gt;
            &lt;xsl:apply-templates select="DataArea/Image/Detail"/&gt;
          &lt;/Document&gt;
        &lt;/Archive&gt;
      &lt;/Import&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="Detail"&gt;
      &lt;DocFile FileLoc="C:\SHARE FOLDER\SCANNED FILES\Scan{format-number(position() - 1, '0000')}.tif" /&gt; &lt;!-- Get the Filepath, give it a filename based on its position in the XML --&gt;
      &lt;Fields&gt;
        &lt;xsl:apply-templates select="parent::Image/Header/*[not(self::Count)]"/&gt; &lt;!-- Get Header Data, Ignore Count Node if Present, Apply Templates at bottom of XSL --&gt;
        &lt;xsl:apply-templates select="*"/&gt;
      &lt;/Fields&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="Header/* | Detail/*[not(self::ImageData)]"&gt;  &lt;!-- Match Header OR Detail Child Elements --&gt;
      &lt;Field Name="{name()}" value="{.}" pass="True"/&gt;
    &lt;/xsl:template&gt;
  &lt;!-- Get Image File --&gt;
    &lt;xsl:template match="Detail/ImageData"&gt;
     &lt;xsl:variable name="img" select="concat('file:///c:/test', format-number(count(parent::Detail/preceding-sibling::Detail), '0000'), '.jpg')"/&gt;
    &lt;xsl:variable name="fos" select="fos:new(string($img))"/&gt;
     &lt;xsl:value-of select="
       fos:write($fos,
       saxon:base64Binary-to-octets(xs:base64Binary(.)))"/&gt;
     &lt;xsl:value-of select="fos:close($fos)"/&gt;
  &lt;/xsl:template&gt;
   &lt;/xsl:stylesheet&gt;
  </code></pre>