问题描述
我已经看到几个关于如何在 base64 中对图像文件进行编码的问题,但反过来如何 - 如何从存储在 XML 文件中的 base64 字符串重建图片?
I have seen several questions on how to encode an image file in base64, but how about the other way around - how do I reconstitute a picture from a base64 string stored in an XML file?
<resource>
<data encoding="base64">
R0lGODlhEAAQAPMAMcDAwP/crv/erbigfVdLOyslHQAAAAECAwECAwECAwECAwECAwECAwECAwEC
AwECAyH/C01TT0ZGSUNFOS4wGAAAAAxtc09QTVNPRkZJQ0U5LjAHgfNAGQAh/wtNU09GRklDRTku
MBUAAAAJcEhZcwAACxMAAAsTAQCanBgAIf8LTVNPRkZJQ0U5LjATAAAAB3RJTUUH1AkWBTYSQXe8
fQAh+QQBAAAAACwAAAAAEAAQAAADSQhgpv7OlDGYstCIMqsZAXYJJEdRQRWRrHk2I9t28CLfX63d
ZEXovJ7htwr6dIQB7/hgJGXMzFApOBYgl6n1il0Mv5xuhBEGJAAAOw==
</data>
<mime>image/gif</mime>
<resource-attributes>
<file-name>clip_image001.gif</file-name>
</resource-attributes>
</resource>
给定上述 XML 节点 resource
,我该如何创建 clip_image001.gif
?
Given the above XML node resource
, how do I go about creating clip_image001.gif
?
请提出建议:
- XSLT 处理器和/或扩展支持这一点,另外
- 一个示例 XSLT,它触发转化
请注意,它必须至少能够处理 GIF &PNG 文件格式.最好不限于任何操作系统.
Note that it must be able to handle at least GIF & PNG file formats. Preferably not restricted to any OS.
基于Mads Hansen 的解决方案.主要区别在于我直接在我的命名空间中引用了 net.sf.saxon.value.Base64BinaryValue
而不是使用 saxon
命名空间,因为我比Saxonica 网站对 base64Binary-to-octets
和 base64Binary
函数的描述.
Based around Mads Hansen's solution. Main difference being that I referenced net.sf.saxon.value.Base64BinaryValue
directly in my namespace rather than using the saxon
namespace, because I understood the Java APIs more intuitively than the Saxonica website's descriptions of the base64Binary-to-octets
and base64Binary
functions.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b64="net.sf.saxon.value.Base64BinaryValue"
xmlns:fos="java.io.FileOutputStream"
...
exclude-result-prefixes="b64 fos">
...
<xsl:for-each select="resource">
<xsl:variable name="b64" select="b64:new(string(data))"/>
...
<xsl:variable name="fos" select="fos:new(string($img))"/>
<xsl:value-of select="fos:write($fos, b64:getBinaryValue($b64))"/>
<xsl:value-of select="fos:close($fos)"/>
</xsl:for-each>
...
附言请参阅兄弟问题,了解我如何获取识别图像文件所需的哈希值.
P.S. See sibling question for my implementation of how to obtain the hashes necessary to identify the image files.
这个问题是我之前问过的另一个问题的子问题.
推荐答案
我从 XSL 主要列表 中找到了这个条目,它描述了如何使用 Saxon 扩展函数 xs:base64Binary-to-octet 将其流式传输到在 XSLT 2.0 样式表中使用 Java FileOutputStream 的文件:
I found this entry from the XSL maiing lists that describes how to use the Saxon extension function xs:base64Binary-to-octet to stream it out to a file using the Java FileOutputStream in an XSLT 2.0 stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:saxon="http://saxon.sf.net/";
xmlns:fos="java.io.FileOutputStream">
<xsl:template match="/">
<xsl:variable name="img" select="concat('c: estjesper', '.jpg')"/>
<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>
这篇关于XSLT:将 base64 数据转换为图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!