ISBN: 0596006349

1st edition, , O'Reilly Media, Inc.

Chapter 17: Working with strings

Useful function: contains-word (see also functx:contains-word)
declare namespace functx = "http://www.functx.com";
declare function functx:contains-word
($string as xs:string?, $word as xs:string) as xs:boolean
    let $upString := upper-case($string)
    let $upWord := upper-case($word)
    return matches($upString, concat("^(.*\W)?", $upWord, "(\W.*)?$"))
Table 17-2. Examples of contains, starts-with, and ends-with
ExampleReturn value
contains("query", "ery")true
contains("query", "query")true
contains("query", "x")false
starts-with("query", "que")true
starts-with("query", "u")false
ends-with("query", "y")true
ends-with("query ", "y")false
Table 17-3. Examples of the matches function
ExampleReturn value
matches("query", "q")true
matches("query", "qu")true
matches("query", "xyz")false
matches("query", "q.*")true
matches("query", "[a-z]{5}")true
Useful function: substring-after-last (see also functx:substring-after-last)
declare namespace functx = "http://www.functx.com";
declare function functx:substring-after-last
($string as xs:string?, $delim as xs:string) as xs:string?
    if (contains ($string, $delim))
    then functx:substring-after-last(substring-after($string, $delim), $delim)
    else $string
Table 17-4. Examples of the substring functions
ExampleReturn value
substring("query", 2, 3)uer
substring("query", 2)uery
substring-before("query", "er")qu
substring-before("queryquery", "er")qu
substring-after("query", "er")y
substring-after("queryquery", "er")yquery
Useful function: set-string-to-length (see also functx:pad-string-to-length)
declare namespace functx = "http://www.functx.com";
declare namespace functx = "http://www.functx.com";
declare function functx:set-string-to-length
($stringToPad as xs:string?, $padChar as xs:string,
 $length as xs:integer) as xs:string
      string-join (($stringToPad, for $i in (1 to $length) return $padChar),"")
Table 17-5. Examples of the string-length function
ExampleReturn value
string-length("  query  ")9
string-length(normalize-space(" query "))5
string-length(" ")1
Table 17-7. Examples of the tokenize function
ExampleReturn value
tokenize("a b c", "\s")("a", "b", "c")
tokenize("a b c", "\s+")("a", "b", "c")
tokenize("a-b—c", "-")("a", "b", "", "c")
tokenize("-a-b-", "-")("", "a", "b", "")
tokenize("a/ b/ c", "[/\s]+")("a", "b", "c")
tokenize("2006-12-25T12:15:00", "[\-T:]")("2006","12","25","12","15","00")
tokenize("Hello, there.", "\W+")("Hello", "there")
Table 17-9. Examples of the upper-case and lower-case functions
ExampleReturn value
Table 17-10. Examples of the replace function
ExampleReturn value
replace("query", "r", "as")queasy
replace("query", "qu", "quack")quackery
replace("query", "[ry]", "l")quell
replace("query", "[ry]+", "l")quel
replace("query", "z", "a")query
replace("query", "query", "")A zero-length string
Useful function: replace-first (see also functx:replace-first)
declare namespace functx = "http://www.functx.com";
declare function functx:replace-first
($string as xs:string?, $pattern as xs:string,
 $replacement as xs:string) as xs:string
    replace($string, concat("(^.*?)", $pattern), concat("$1",$replacement))
Table 17-11. Examples of the normalize-space function
ExampleReturn value
normalize-space(" query ")query
normalize-space("xml query")xml query
normalize-space("xml query")xml query
normalize-space(" ")A zero-length string
Datypic XQuery Services