XSLT:将base64数据转换为图像文件

发布于 2024-08-09 23:39:35 字数 2229 浏览 6 评论 0原文

我已经看到了几个关于如何用 Base64 编码图像文件的问题,但是反过来怎么样 - 如何从存储在 XML 文件中的 Base64 字符串重建图片?

<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 节点资源,我该如何创建clip_image001.gif

请建议:

  1. XSLT 处理器和/或扩展启用此功能,再加
  2. 上触发该功能的示例 XSLT 转换

注意它必须至少能够处理GIF & PNG 文件格式。最好不限于任何操作系统。


实施的解决方案

基于 Mads Hansen 解决方案。主要区别在于,我直接在命名空间中引用了 net.sf.saxon.value.Base64BinaryValue,而不是使用 saxon 命名空间,因为我对 Java API 的理解比对 Java API 的理解更直观。 Saxonica 网站对 base64Binary-to-octetsbase64Binary 函数的描述。

<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>
...

PS 请参阅兄弟问题了解我如何实施获取识别图像文件所需的哈希值。


This question is a subquestion of another question I have asked previously.

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>

Given the above XML node resource, how do I go about creating clip_image001.gif?

Please suggest:

  1. XSLT processors and/or extensions enable this, plus
  2. a sample XSLT that triggers the
    conversion

Note that it must be able to handle at least GIF & PNG file formats. Preferably not restricted to any OS.


Implemented solution

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.


This question is a subquestion of another question I have asked previously.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

噩梦成真你也成魔 2024-08-16 23:39:35

我从 XSL 邮件列表中找到了此条目,它描述了如何使用 Saxon 扩展函数 xs:base64Binary-to-octet 将其通过 XSLT 2.0 样式表中的 Java FileOutputStream 流式传输到文件:

<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:\test\jesper', '.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>

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:\test\jesper', '.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>
大姐,你呐 2024-08-16 23:39:35

以下作品:

<img>
  <xsl:attribute name="src">
    <xsl:value-of select="concat('data:image/gif;base64,',xPath)"/>
  </xsl:attribute>
</img>

The following works:

<img>
  <xsl:attribute name="src">
    <xsl:value-of select="concat('data:image/gif;base64,',xPath)"/>
  </xsl:attribute>
</img>
梦醒时光 2024-08-16 23:39:35

将其转换为 HTML。

<img src="data:{mime};base64,{data}" />

Transform it to HTML.

<img src="data:{mime};base64,{data}" />
花落人断肠 2024-08-16 23:39:35

从 Saxon 9.5 开始,通过 EXPath 文件扩展模块(在 Saxon-PE 和 Saxon-EE 中可用)提供了更好的方法。

下面是我用来从 Word 文档中提取二进制图像文件的代码片段(源 XML 为 WordProcessingML 格式):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:file="http://expath.org/ns/file" xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">

<xsl:template match="/pkg:package">
    <xsl:apply-templates select="pkg:part/pkg:binaryData"/>
</xsl:template>

<xsl:template match="pkg:binaryData">
    <xsl:variable name="filename">
        <xsl:value-of select="replace(../@pkg:name, '/word/media/', '')"/>
    </xsl:variable>
    <xsl:variable name="path" select="concat('/some/folder/', $filename)"/>
    <xsl:message><xsl:value-of select="$path"/></xsl:message>

    <xsl:value-of select="file:write-binary($path, xs:base64Binary(string()))"/>       
</xsl:template>

There is a better method available since Saxon 9.5 via the EXPath File extension module (available in Saxon-PE and Saxon-EE).

Here is a fragment of the code I'm using to extract binary image files from Word documents (source XML is in WordProcessingML format):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:file="http://expath.org/ns/file" xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">

<xsl:template match="/pkg:package">
    <xsl:apply-templates select="pkg:part/pkg:binaryData"/>
</xsl:template>

<xsl:template match="pkg:binaryData">
    <xsl:variable name="filename">
        <xsl:value-of select="replace(../@pkg:name, '/word/media/', '')"/>
    </xsl:variable>
    <xsl:variable name="path" select="concat('/some/folder/', $filename)"/>
    <xsl:message><xsl:value-of select="$path"/></xsl:message>

    <xsl:value-of select="file:write-binary($path, xs:base64Binary(string()))"/>       
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文