Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Xquery Functions in XML

Xquery Functions in XML

XQuery Functions in XML


What are XQuery Functions?

  • XQuery provides built-in functions to manipulate XML data, strings, numbers, dates, and more.

  • Functions help perform operations like string manipulation, node handling, mathematical calculations, and sequence processing.

  • You can also define your own user-defined functions.


Commonly Used Built-in XQuery Functions

FunctionPurposeExampleResult
fn:string()Converts value to stringfn:string(123)"123"
fn:concat()Concatenates stringsfn:concat("Hello", " ", "World")"Hello World"
fn:contains()Checks if a string contains a substringfn:contains("Hello World", "World")true
fn:substring()Returns a substringfn:substring("Hello", 2, 3)"ell"
fn:count()Counts nodes or items in a sequencefn:count(/library/book)3
fn:starts-with()Checks if a string starts with a substringfn:starts-with("Hello", "He")true
fn:upper-case()Converts string to uppercasefn:upper-case("hello")"HELLO"
fn:lower-case()Converts string to lowercasefn:lower-case("HELLO")"hello"
fn:not()Returns logical NOTfn:not(true())false
fn:substring-before()Returns substring before a given stringfn:substring-before("2025-06-04", "-")"2025"
fn:substring-after()Returns substring after a given stringfn:substring-after("2025-06-04", "-")"06-04"

Example: Using Functions in XQuery

Given this XML:

<library>  <book category="fiction">    <title>The Hobbit</title>  </book>  <book category="history">    <title>Sapiens</title>  </book></library>

XQuery:

for $b in /library/bookwhere fn:contains($b/@category, "fiction")return fn:upper-case($b/title/text())

Result:

THE HOBBIT

Defining Your Own Function

declare function local:greet($name as xs:string) as xs:string {  concat("Hello, ", $name, "!")};local:greet("Vikash")

Output:

Hello, Vikash!

Summary

  • Use built-in functions for string, number, boolean, and node operations.

  • Functions help make XQuery expressions more powerful and concise.

  • You can declare your own functions for reusable code blocks.


Want examples for specific functions or help writing your own functions?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql