Skip to content

Instantly share code, notes, and snippets.

@line-o
Last active October 9, 2024 15:55
Show Gist options
  • Save line-o/1577c3898264b56f52ff3341294f780e to your computer and use it in GitHub Desktop.
Save line-o/1577c3898264b56f52ff3341294f780e to your computer and use it in GitHub Desktop.
xquery version "3.1";
declare namespace enc="//-o/ns/xquery/encoding";
import module namespace bin="http://expath.org/ns/binary";
declare function enc:fix ($string as xs:string) as xs:string {
analyze-string($string, "(\\u00([0-9a-z]{2})\\u00([0-9a-z]{2}))")/element()
=> for-each(enc:process#1)
=> string-join("")
};
declare %private function enc:process ($el as element()) as xs:string {
typeswitch ($el)
case element(fn:match) return enc:replace($el/fn:group/fn:group)
default return $el
};
declare %private function enc:replace ($parts as xs:string+) as xs:string {
string-join($parts, "")
=> xs:hexBinary()
=> bin:decode-string("utf8")
};
enc:fix("Pierre Roger, Cl\u00c3\u00a9ment VI")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment