将字符串转换为二进制base64

问题描述 投票:0回答:1

有没有办法如何将字符串转换为二进制base64?我见过很多参考文献,但它最终没有用。例如,我有这个输入文件:

<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <Data>
         <Binary>
               <RawData>This element should convert string to binary base64.</RawData>
         </Binary>
    </Data>
</RootElement>

我需要生成:

<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Data>
    <Binary>
        <RawData>VGhpcyBlbGVtZW50IHNob3VsZCBjb252ZXJ0IHN0cmluZyB0byBiaW5hcnkgYmFzZTY0Lg==</RawData>
    </Binary>
</Data>

我创建了一个xslt并使用了我在网上看到的命名空间:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dp="http://www.datapower.com/extensions">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="RawData">
    <xsl:element name="RawData">
        <xsl:value-of select="dp:encode(., 'base-64')"/>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

谢谢。

xslt base64 xslt-1.0 xslt-2.0
1个回答
2
投票

有一个适用于任何XSLT处理器的纯XSLT 1.0解决方案:JAXP,Saxon,Xalan,Xsltproc,Microsoft:

  1. 下载base64.xsl
  2. 下载base64_binarydatamap.xml
  3. 使用XSLT 1.0: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b64="https://github.com/ilyakharlamov/xslt_base64"> <xsl:output method="xml"/> <xsl:include href="base64.xsl"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="/RootElement/Data/Binary/RawData"> <xsl:call-template name="b64:encode"> <xsl:with-param name="asciiString" select="text()"/> </xsl:call-template> </xsl:template> </xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.