简体   繁体   English

将XML中的Base64图像写入文件

[英]Writing Base64 Images in an XML to a File

I have a XML which contains Base64 data, what's the best way to strip it out using a XSLT file? 我有一个包含Base64数据的XML,使用XSLT文件将其删除的最佳方法是什么? I am using Saxon to reformat the XML and would want to include a filepath in the formatted XML. 我正在使用Saxon重新格式化XML,并希望在格式化的XML中包括文件路径。 I looked at a few examples on SO, but can't seem to get it working. 我看了一些关于SO的示例,但似乎无法使其正常工作。

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. 我试图提取Base64编码的数据并将其写入外部文件,然后以编程方式将其位置写入我输出的XML中。 I may have multiple elements of the same name that contain base64 image data. 我可能有多个包含base64图像数据的同名元素。 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. 根据我在网上找到的一些示例以及专家的帮助,我在下面有一些代码,但是返回错误,这是我收到的错误的屏幕截图。 http://i.imgur.com/5Np9bNp.png http://i.imgur.com/5Np9bNp.png

<?xml version="1.0" encoding="utf-8"?>
<ProcessArea>
<DataArea>
        <Process confirm="Always" acknowledge="Never"/>
        <Image>
<Detail>
                <TypeId>1</TypeId>
                <Description>Description</Description>
                <ImageId>525225</ImageId>
                <New>true</New>
                <PageCount>2</PageCount>
                <ImageData><![CDATA[BASE64String==]]></ImageData>
            </Detail>
        </Image>
    </DataArea>
</ProcessImage>

Below is the XSLT File I am using. 以下是我正在使用的XSLT文件。

   <!-- 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>
<!-- Get Image File -->
  <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>

It's not clear from your question what output you want to produce. 从您的问题尚不清楚您要产生什么输出。

There are some useful Saxon extension functions for handling base64 data: see 有一些有用的Saxon扩展功能可用于处理base64数据:请参见

http://www.saxonica.com/documentation/#!functions/saxon http://www.saxonica.com/documentation/#!functions/saxon

and

http://www.saxonica.com/documentation/#!functions/saxon http://www.saxonica.com/documentation/#!functions/saxon

For example if you want to write a binary file, first convert the base64 string to an xs:base64Binary value using the xs:base64Binary constructor, then write to the file using the EXPath file:writeBinary() function. 例如,如果要编写二进制文件,请首先使用xs:base64Binary构造函数将base64字符串转换为xs:base64Binary值,然后使用EXPath file:writeBinary()函数将其写入文件。

You will need at least Saxon-PE. 您至少需要Saxon-PE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM