XPath String Functions Tutorial 🎯

beginner
16 min

XPath String Functions Tutorial 🎯

Welcome to our XPath String Functions tutorial! In this comprehensive guide, we'll explore various string functions that help you manipulate and extract data from XML documents with ease. By the end of this tutorial, you'll be able to perform powerful text operations using XPath string functions. 💡 Pro Tip: XPath functions are essential for handling text data in XML files, especially when working with large datasets or complex structures.

Table of Contents

  1. Introduction to XPath String Functions
  2. Common XPath String Functions
  3. Practical Examples with Code Samples

<a name="introduction"></a>

1. Introduction to XPath String Functions 📝

XPath string functions are a set of built-in functions that allow you to perform various text manipulations on XML elements and attributes. These functions are indispensable when dealing with text data in XML files, as they enable you to search, extract, and manipulate text data effectively.

<a name="common"></a>

2. Common XPath String Functions

In this section, we'll dive into some essential XPath string functions. Let's get started!

2.1 concat() 📝

The concat() function concatenates two or more strings into a single string.

Syntax: concat(string1, string2, ...)

Example:

xml
<root> <name>John</name> <surname>Doe</surname> </root> XPath expression: concat(//name, ' ', //surname) Result: John Doe

2.2 substring() 📝

The substring() function extracts a substring from a given string, starting at a specified position and returning a specified length.

Syntax: substring(string, start, length)

Example:

xml
<root> <fullname>John Doe</fullname> </root> XPath expression: substring(//fullname, 6, 4) Result: Doe

2.3 substring-before() 📝

The substring-before() function extracts the substring before the specified position.

Syntax: substring-before(string, delimiter)

Example:

xml
<root> <email>john.doe@example.com</email> </root> XPath expression: substring-before(//email, '@') Result: john.doe

2.4 substring-after() 📝

The substring-after() function extracts the substring after the specified delimiter.

Syntax: substring-after(string, delimiter)

Example:

xml
<root> <url>http://www.example.com</url> </root> XPath expression: substring-after(//url, 'http://') Result: www.example.com

2.5 contains() 📝

The contains() function checks if a substring exists within the given string.

Syntax: contains(string, substring)

Example:

xml
<root> <message>Hello World</message> </root> XPath expression: contains(//message, 'World') Result: true

2.6 starts-with() 📝

The starts-with() function checks if the given string starts with the specified substring.

Syntax: starts-with(string, substring)

Example:

xml
<root> <filename>example_123.xml</filename> </root> XPath expression: starts-with(//filename, 'example') Result: true

2.7 ends-with() 📝

The ends-with() function checks if the given string ends with the specified substring.

Syntax: ends-with(string, substring)

Example:

xml
<root> <filename>example_123.xml</filename> </root> XPath expression: ends-with(//filename, '.xml') Result: true

2.8 normalize-space() 📝

The normalize-space() function removes unnecessary whitespaces (spaces, tabs, line breaks) from the given string and replaces consecutive whitespaces with a single space.

Syntax: normalize-space(string)

Example:

xml
<root> <message> Hello World </message> </root> XPath expression: normalize-space(//message) Result: Hello World

<a name="examples"></a>

3. Practical Examples with Code Samples 💡

Let's put XPath string functions into practice with two examples that demonstrate their real-world usage.

Example 1: Extracting a person's full name from an XML file

xml
<people> <person> <firstname>John</firstname> <lastname>Doe</lastname> </person> <!-- More persons... --> </people>

XPath expression: concat(//person/firstname, ' ', //person/lastname) Result: John Doe

Example 2: Finding all emails with the .com domain in an XML file

xml
<emails> <email>john.doe@example.com</email> <email>jane.smith@yahoo.co.uk</email> <!-- More emails... --> </emails>

XPath expression: //email[contains(., '@') and ends-with(., '.com')] Result: <email>john.doe@example.com</email>

Quick Quiz
Question 1 of 1

What does the XPath expression `concat(//person/firstname, ' ', //person/lastname)` do in the given XML example?

By mastering XPath string functions, you'll be well-equipped to manipulate text data in your XML documents efficiently and effectively. Happy coding! 🤖💻