【问题标题】:Writing Base64 Images in an XML to a File将 XML 中的 Base64 图像写入文件
【发布时间】:2013-06-26 02:36:28
【问题描述】:

我有一个包含 Base64 数据的 XML,使用 XSLT 文件去除它的最佳方法是什么?我正在使用 Saxon 重新格式化 XML,并希望在格式化的 XML 中包含文件路径。我查看了一些关于 SO 的示例,但似乎无法使其正常工作。

我正在尝试提取 Base64 编码数据并将其写入外部文件,然后以编程方式将其位置写入我输出的 XML 中。我可能有多个包含 base64 图像数据的同名元素。根据我在网上找到的一些示例和专家的帮助,我在下面有一些代码,但它返回错误,这是我得到的错误的屏幕截图。 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>

下面是我正在使用的 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>

【问题讨论】:

  • 您需要编辑您的问题以包含更多如何它不起作用的详细信息:输出是什么,您希望输出是什么?

标签: xml xslt base64 saxon


【解决方案1】:

从你的问题中不清楚你想要产生什么输出。

有一些有用的 Saxon 扩展函数可用于处理 base64 数据:请参阅

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

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

例如,如果要写入二进制文件,首先使用 xs:base64Binary 构造函数将 base64 字符串转换为 xs:base64Binary 值,然后使用 EXPath file:writeBinary() 函数写入文件。

您至少需要 Saxon-PE。

【讨论】:

    猜你喜欢
    • 2014-01-19
    • 2014-08-01
    • 1970-01-01
    • 2013-01-31
    • 2016-01-13
    • 1970-01-01
    • 2017-09-09
    • 2013-12-14
    • 1970-01-01
    相关资源
    最近更新 更多