xml
  1. xml-xquery-string-functions

XML XQuery String functions

  • XQuery provides a set of built-in functions that helps in processing and manipulating strings.
  • These functions can be used on string values or expressions in XQuery.
  • The following are some of the commonly used XQuery String Functions.

fn:string()

Syntax

fn:string($arg as item()*) as xs:string

Example

let $str := <name>John Doe</name>
return string($str)

Output

John Doe

Explanation

The fn:string() function is used to convert any item to its string representation. In the example above, a string literal is wrapped in an XML element. By passing the element to the fn:string() function, the string representation of the element is returned.

fn:string-length()

Syntax

fn:string-length($arg as xs:string?) as xs:integer

Example

let $str := "John Doe"
return string-length($str)

Output

8

Explanation

The fn:string-length() function returns the number of characters in a string. In the example above, the length of the string "John Doe" is 8 characters.

fn:substring()

Syntax

fn:substring($sourceString as xs:string?, $startingLoc as xs:integer, $length as xs:integer?) as xs:string

Example

let $str := "John Doe"
return fn:substring($str, 1, 4)

Output

John

Explanation

The fn:substring() function returns a portion of a string based on the starting location and length specified. In the example above, the function returns the substring starting at the first character and continuing for 4 characters ("John").

fn:substring-after()

Syntax

fn:substring-after($haystack as xs:string?, $needle as xs:string) as xs:string

Example

let $str := "John Doe"
return fn:substring-after($str, " ")

Output

Doe

Explanation

The fn:substring-after() function returns the substring that appears after a specified substring. In the example above, the function returns the substring after the space character ("Doe").

fn:substring-before()

Syntax

fn:substring-before($haystack as xs:string?, $needle as xs:string) as xs:string

Example

let $str := "John Doe"
return fn:substring-before($str, " ")

Output

John

Explanation

The fn:substring-before() function returns the substring that appears before a specified substring. In the example above, the function returns the substring before the space character ("John").

fn:replace()

Syntax

fn:replace($input as xs:string?, $pattern as xs:string, $replacement as xs:string) as xs:string

Example

let $str := "hello world"
return fn:replace($str, "world", "there")

Output

hello there

Explanation

The fn:replace() function is used to replace all occurrences of a specified pattern with a replacement string. In the example above, all occurrences of the string "world" are replaced with "there".

Use

XQuery String Functions are used to manipulate and process string values in XQuery. They can be used in a variety of ways, such as parsing and transforming data, formatting output, and performing string manipulation operations.

Important Points

  • XQuery provides a set of built-in functions that helps in processing and manipulating strings.
  • These functions can be used on string values or expressions in XQuery.
  • Some of the commonly used XQuery String Functions include fn:string(), fn:string-length(), fn:substring(), fn:substring-after(), fn:substring-before(), and fn:replace().

Summary

In this XQuery String Functions tutorial, we have covered various string functions that can be used to manipulate and process strings in XQuery. These functions can be used for a wide variety of purposes, such as parsing and transforming data, formatting output, and performing string manipulation operations.

Published on: