kcho0
kcho0

Reputation: 169

How to decode base64 string as HTML content in XSLT?

I'm trying to convert my encoded base64 HTML content from an attribute and place it in the HTML content using XLST, hope somebody can help.

I have this string:

<p><b>Hello!</b><span>This is a span</span></p>

And this is the base64 encoded value:

PHA+PGI+SGVsbG8hPC9iPjxzcGFuPlRoaXMgaXMgYSBzcGFuPC9zcGFuPjwvcD4=

The XML data looks like this:

<Data Get="True">
    <Result TextFlow="PHA+PGI+SGVsbG8hPC9iPjxzcGFuPlRoaXMgaXMgYSBzcGFuPC9zcGFuPjwvcD4=">
        </Result>
</Data>

My last attempt was using this:

fn:cast($XML/Data/Result/@TextFlow,'xs:base64Binary','xs:String',false())

That code send this error:
Unknown function - Name and number or arguments do not match any function signature in the static context.

Thanks for your help!

Upvotes: 5

Views: 11121

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

There's no built-in capability in XSLT to do this.

Coming soon is the EXPath binary module:

http://expath.org/spec/binary

The bin:decode-string() function is what you need. We've got an implementation of this for Saxon but it's not integrated into the product yet. Meanwhile there is the extension function saxon:base64BinaryToString which is essentially the same:

http://www.saxonica.com/documentation/index.html#!functions/saxon/base64Binary-to-string

and which is available in Saxon-PE or higher.

Both functions take an argument of type xs:base64Binary; to convert a string in base64 to a value of this type, you use the constructor function xs:base64Binary(string). This requires XSLT 2.0 of course.

If your (decoded) string contains markup, then to copy it to the serialized HTML output you will need to use the deprecated disable-output-escaping="yes" option.

UPDATE (April 2019)

Recent releases of Saxon (the current release is 9.9) include the EXPath binary module as standard in Saxon-PE and higher editions.

Upvotes: 4

Related Questions