Solved

Processing Base 64 Image in XML

Posted on 2013-06-25
1
397 Views
Last Modified: 2013-07-23
Experts,

How can I process Binary ImageData using Saxon and output a file?  I want to include the filepath in the XML to the image.

Below is my XSLT file, which works perfectly and attached is a sample XML.

<!-- Begin XST Style Sheet -->


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" exclude-result-prefixes="saxon" xmlns:fos="java.io.FileOutputStream">
  
  <!-- Indent in the output -->
  <xsl:output indent="yes"/>
  
  <!-- Match the root -->
  <xsl:template match="ProcessImage">
    <Import> <!-- Insert our Import Node -->
      <Archive ConnectionID="1" Name="1"> <!-- Define our Application Connection String -->
        <Document pass="True"> <!-- Statically set documents to pass -->
          <xsl:for-each select="Pages/Page"> <!-- Ignore -->
            <DocFile FileLoc="{@FileName}" /> <!-- Ignore -->
          </xsl:for-each>
          <xsl:apply-templates select="DataArea/Image/Detail"/>
        </Document>
      </Archive>
    </Import>
  </xsl:template>
  
  <xsl:template match="Detail">
    <DocFile FileLoc="C:\SHARE FOLDER\SCANNED FILES\Scan{format-number(position() - 1, '0000')}.tif" /> <!-- Get the Filepath, give it a filename based on its position in the XML -->
    <Fields>
      <xsl:apply-templates select="parent::Image/Header/*[not(self::Count)]"/> <!-- Get Header Data, Ignore Count Node if Present, Apply Templates at bottom of XSL -->
      <xsl:apply-templates select="*[not(self::ImageData)]"/> <!-- Ignore Image Data Node if Present -->
    </Fields>
  </xsl:template>
  
  <xsl:template match="Header/* | Detail/*">  <!-- Match Header OR Detail Child Elements -->
    <Field Name="{name()}" value="{.}" pass="True"/>
  </xsl:template>
  <!-- Get Image Data
  <xsl:template match="ProcessImage">
   <xsl:variable name="img" select="ImageData"/>
   <xsl:variable name="fos" select="fos:new(string($img))"/>
   <xsl:value-of select="fos:write($fos,saxon:base64Binary-to-octets(xs:base64Binary(my-base64-encoded-image)))"/>
   <xsl:value-of select="fos:close($fos)"/>
</xsl:template>
  -->
</xsl:stylesheet>

Open in new window

sample.xml
0
Comment
Question by:lm1189
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39277504
<!-- Begin XST Style Sheet -->
<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">
  
  <!-- Indent in the output -->
  <xsl:output indent="yes"/>
  
  <!-- Match the root -->
  <xsl:template match="ProcessImage">
    <Import> <!-- Insert our Import Node -->
      <Archive ConnectionID="1" Name="1"> <!-- Define our Application Connection String -->
        <Document pass="True"> <!-- Statically set documents to pass -->
          <xsl:for-each select="Pages/Page"> <!-- Ignore -->
            <DocFile FileLoc="{@FileName}" /> <!-- Ignore -->
          </xsl:for-each>
          <xsl:apply-templates select="DataArea/Image/Detail"/>
        </Document>
      </Archive>
    </Import>
  </xsl:template>
  
  <xsl:template match="Detail">
    <DocFile FileLoc="C:\SHARE FOLDER\SCANNED FILES\Scan{format-number(position() - 1, '0000')}.tif" /> <!-- Get the Filepath, give it a filename based on its position in the XML -->
    <Fields>
      <xsl:apply-templates select="parent::Image/Header/*[not(self::Count)]"/> <!-- Get Header Data, Ignore Count Node if Present, Apply Templates at bottom of XSL -->
      <xsl:apply-templates select="*"/>
    </Fields>
  </xsl:template>
  
  <xsl:template match="Header/* | Detail/*[not(self::ImageData)]">  <!-- Match Header OR Detail Child Elements -->
    <Field Name="{name()}" value="{.}" pass="True"/>
  </xsl:template>

  <xsl:template match="Detail/ImageData">
   <xsl:variable name="img" select="concat('file:///c:/test', format-number(count(parent::Detail/preceding-sibling::Detail), '0000'), '.jpg')"/>
  <xsl:variable name="fos" select="fos:new(string($img))"/>
   <xsl:value-of select="
     fos:write($fos,
     saxon:base64Binary-to-octets(xs:base64Binary(.)))"/>
   <xsl:value-of select="fos:close($fos)"/>
</xsl:template>
 </xsl:stylesheet>

Open in new window


I changed your code a bit so at least the XSLT is correct and does what you expect.
But I can't get it to work since I somewhere have a second copy of FileOutputStream on my classpath and I can't get that message out
Note that your base64 snippets are not correct too
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now