Solved

Processing Base 64 Image in XML

Posted on 2013-06-25
1
396 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
Comment Utility
<!-- 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

12 Experts available now in Live!

Get 1:1 Help Now