xslt 1.0 base64编码模板的内容 [英] xslt 1.0 base64 encode the content of a template

查看:101
本文介绍了xslt 1.0 base64编码模板的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用XSLT 1.0在base64中编码模板的内容?

How can I encode the content of a template in base64, using XSLT 1.0?

使用序列化模式,在PHP环境中运行

using serialization mode, runing in PHP enviroment

就像我有一个这样的模板:

It's like i have a template like this:

<xsl:template name="test">
    <test 
      gender="male" 
      name1="TEST" 
      name2="TEST">
      <sometags>
            <tag></tag>
        </sometags>
    </test>
</xsl:template>

并且我希望输出是这样的:

and I want the output to be like this:

<base64>PHRlc3QgDQoJCSAgZ2VuZGVyPSJtYWxlIiANCgkJICBuYW1lMT0iVEVTVCIgDQoJCSAgbmFtZTI9IlRFU1QiPg0KICAgICAgICAgIDxzb21ldGFncz4NCgkJCQk8dGFnPjwvdGFnPg0KCQkJPC9zb21ldGFncz4NCgkJPC90ZXN0Pg==</base64>

推荐答案

Mukhul Gandhi 创建了一个在XSLT 1.0中运行的Base64编码器.如果可以切换到XSLT 2.0,则可以创建样式表函数来执行相同的操作.

Mukhul Gandhi created a Base64 encoder that runs in XSLT 1.0. If you can switch to XSLT 2.0, you can create stylesheet functions to do the same.

但是,由于您似乎打算将节点编码为字符串,因此不应创建节点,而应创建字符串:

However, because you seem to mean to encode nodes into strings, you should not create nodes, but strings instead:

使用 node-set扩展功能重新应用模板的结果 (几乎受所有XSLT 1.0处理器支持)并编写如下代码:

Re-apply the result of your template using the node-set extension function (supported by (almost?) all XSLT 1.0 processors) and write something like this:

<xsl:template match="*">
    <xsl:text>&lt;</xsl:text>
    <xsl:value-of select="name()" />
    <xsl:apply-templates select="@*" />
    <xsl:text>&gt;</xsl:text>
    <xsl:apply-templates />
    <xsl:text>&lt;/</xsl:text>
    <xsl:value-of select="name()" />
    <xsl:text>&gt;</xsl:text>
</xsl:template>

<xsl:template match="@*">
    <xsl:text> </xsl:text>
    <xsl:value-of select="name()" />
    <xsl:text>="</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>"</xsl:text>
</xsl:template>

注意:未经测试,您可能希望扩展它以添加缩进,处理其他节点(如处理指令和注释),并在属性的情况下,以转义字符串中的所有引号.

Note: not tested, and you probably want to extend it to add indentation, processing of other nodes like processing instructions and comments, and in the case of attributes, to escape any quotes in the strings.

在XSLT 3.0中,您可以使用 fn:serialize功能.

In XSLT 3.0 you can achieve the same using the fn:serialize function.

这篇关于xslt 1.0 base64编码模板的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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