使用xslt解码base64图像数据 [英] Decoding base64 image data using xslt

查看:102
本文介绍了使用xslt解码base64图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个xml和最多一个xsl样式表,该xml文件的内容如下所示

I am trying to have a single xml and at most one xsl stylesheet, the contents of the xml file are like below

<catalogue>
    <item>
        <item_id>1234</item_id>
        <item_desc>hi-fi sanio</item_desc>
        <price>12.50</price>
        <image>iVBORw0KGgoAAAANSUhEUgAAANIAAAAzCAYAAADigVZlAAA</image>
    </item>
    <item>
        <item_id>4614</item_id>
        <item_desc>lace work</item_desc>
        <price>1.50</price>
        <image>QN0lEQVR4nO2dCXQTxxnHl0LT5jVteHlN+5q+JCKBJITLmHIfKzBHHCCYBAiEw</image>
    </item>
    <item>
        <item_id>614</item_id>
        <item_desc>bicycle</item_desc>
        <price>150</price>
        <image>jVteHlN+5q+JCKBJITLmHIfKzBHHCCYBAiEwlEQVR4nO2dCXQTxxnHl0L</image>
    </item>
</catalogue>

目的是能够与最终用户共享xml文件,以便他们可以使用其Web浏览器(目前为IE11)查看该文件.

The purpose is to be able to share the xml file with end users so that they can view it using their web browser (IE11 for now).

我正在使用python和lxml创建xml文件,我已经使用base64编码对项目的图像进行了编码,并为每个项目添加了一个图像节点. 我真的很想直接将图像嵌入xml中,这样当最终用户打开它时,所有呈现在其浏览器中的图像都将显示在xml中.即没有超链接

I'm using python and lxml to create the xml file I have encoded the image of the items using base64 encoding and added an image node to each item. I really want to embed the images directly in the xml so when an end user opens it up everything renders in their browser. i.e no hyper-links

我对如何使最终用户浏览器解码图像节点感到困惑.他们没有任何可解码的应用程序,因此所有内容都必须使用浏览器在xml或关联的样式表中.

I am at a loss as to how to get end-user browsers to decode the image node. They won't have any application whatsoever to decode so everything must be either in the xml or in an associated stylesheet using the browser.

有人知道是否完全可以使用xsl解码xml文档中嵌入的图像吗? 我可以看的任何示例都可以看到如何实现.

Does anyone know if it is at all possible to use the xsl to decode the image embedded in the xml document? Any examples that I can look at to see how it would be done.

通常,仅使用xml或与xsl结合使用,然后编码(嵌入)图像(png/jpg)的良好方法是什么?

Generally what are good ways to encode and then decode (embed) images (png/jpg) using only xml alone or in conjunction with xsl?

致谢

推荐答案

如果XML中包含base64编码的数据,并且您想在HTML中嵌入呈现该数据的img,则可以构造<img src="data:image/png;base64,{.}"/> XSLT代码中带有data URI的图像元素( https://en.m.wikipedia. org/wiki/Data_URI_scheme )(当然还有图像数据的正确MIME类型),下面是一个示例(假设MIME类型为image/png):

If you have the base64 encoded data in your XML and you want to embed an img rendering that data in your HTML then you can construct an <img src="data:image/png;base64,{.}"/> image element in the XSLT code with a data URI (https://en.m.wikipedia.org/wiki/Data_URI_scheme) (and of course the proper MIME type of the image data), here is an example (assuming the MIME type image/png):

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="html" doctype-public="about:legacy-compat" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
      <html>
        <head>
          <title>data URI test</title>
        </head>
        <body>
          <xsl:apply-templates/>
        </body>
      </html>
    </xsl:template>

    <xsl:template match="catalogue">
        <table>
            <thead>
                <xsl:apply-templates select="item[1]" mode="header"/>
            </thead>
            <tbody>
                <xsl:apply-templates/>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template match="item" mode="header">
        <tr>
            <xsl:apply-templates mode="header"/>
        </tr>
    </xsl:template>

    <xsl:template match="item/*" mode="header">
        <th>
            <xsl:value-of select="local-name()"/>
        </th>
    </xsl:template>

    <xsl:template match="item">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>

    <xsl:template match="item/*">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>

    <xsl:template match="item/image">
        <td>
            <img src="data:image/png;base64,{.}"/>
        </td>
    </xsl:template>

</xsl:transform>

在线访问 http://home.arcor.de/martin.honnen/xslt/test2016080901.xml 分别 http://home.arcor.de /martin.honnen/xslt/test2016080901.xsl ,对于第一个image元素,我使用了Wikipedia文章中的示例图像数据,所有四种浏览器(IE,Edge,Chrome,Firefox)都渲染了嵌入式图像.您输入的XML中的样本数据似乎没有被识别为image/png,我也尝试了image/jpeg,但没有成功,我不确定是哪种图像数据.

Online at http://home.arcor.de/martin.honnen/xslt/test2016080901.xml respectively http://home.arcor.de/martin.honnen/xslt/test2016080901.xsl, for the first image element I have used the example image data from the Wikipedia article and all four browsers (IE, Edge, Chrome, Firefox) render the embedded image. The sample data from your input XML does not seem to be recognized as image/png, I have also tried image/jpeg without any success, I am not sure what kind of image data that is.

这篇关于使用xslt解码base64图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆