Skip to content

Instantly share code, notes, and snippets.

@nine9ths
Created January 8, 2013 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nine9ths/4488993 to your computer and use it in GitHub Desktop.
Save nine9ths/4488993 to your computer and use it in GitHub Desktop.
Replace an uppercase character followed by the lowercase version of itself (e.g. 'Qq') with just the lowercase character in XPath 3.0 with examples in XQuery 3.0 and XSLT 3.0
function($input as xs:string) as xs:string {
fold-left(
function($s,$r){
replace($s,upper-case($r)||$r,$r)
},
$input,
((1 to 26) ! format-integer(.,'a')))
}
let
$collapse.caps :=
function($input as xs:string) as xs:string {
fold-left(
function($s,$r){
replace($s,upper-case($r)||$r,$r)
},
$input,
((1 to 26) ! format-integer(.,'a')))
}
return $collapse.caps('FOOBbar')
(: returns 'FOObar' :)
<xsl:stylesheet
version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:variable name="collapse.caps" as="function(xs:string) as xs:string"
select="
function($input as xs:string) as xs:string {
fold-left(
function($s,$r){
replace($s,upper-case($r)||$r,$r)
},
$input,
((1 to 26) ! format-integer(.,'a')))
}"/>
<xsl:template match="/">
<xsl:sequence select="$collapse.caps('ZzZzZz')"/>
<!-- returns 'zzz' -->
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment