Skip to content

Add facility to serialize binary values as url-safe base64 encoded strings #2381

@line-o

Description

@line-o

In XQuery 3.1 there are two XDM types to represent binary values xs:base64binary and xs:hexBinary.
The current draft adds binary literals as an additional option.

Thus, it is possible to base64 encode any value by casting a xs:base64binary to a xs:string.

xs:base64Binary("+w==") => xs:string()

There is no standard way to serialize those binary values to the URL safe variant of that encoding described in section 5 of RFC 4648.

The simplest workaround is replacing the unsafe characters of the alphabet (+ and /) and dropping the padding at the end with

xs:base64Binary("+w==") => translate("+/=", "-_")

This of course will only work for relatively small binary values. In order for processors to offer a performant and efficient way I see several options.

  1. adding new type xs:base64BinaryUrlSafe whose string representation uses the adapted alphabet with - and _ and does not add padding at the end
  2. a new function in fn namespace fn:encode-base64-url-safe($data as (xs:string | xs:base64Binary | xs:hexBinary)) as xs:string
  3. a new function in bin namespace bin:encode-base64-url-safe($data as (xs:string | xs:base64Binary | xs:hexBinary)) as xs:string
  4. add an output option that will serialize all binary values to base64 url-safe when cast to strings

Addendum

I am also wondering why binary values cannot be created from numeric literals. Especially now that we have the binary notation for integer literals and the xs:integer type is unbounded this would be a perfectly fine literal notation to create binary values from. At least as suitable as string literals that are currently allowed.

xs:hexBinary(0xfb) and xs:base64Binary(0b11111111)

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementA change or improvement to an existing featureXQFOAn issue related to Functions and Operators

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions