Created
January 8, 2013 23:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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