About Mohammad Meraj Zia

I did my Engineering in Information Technology from IET, Lucknow, India. Currently doing MSc in Information Technology from Derby University. I have worked in Java/J2EE domain for the last 10 years. Have good understanding of Payment and Finance domains.

XPath Like Operator Example

In this example we will learn about XPath like operator. We will see how we can perform a ‘like’ operation using XPath. We will make use of IDEA IntelliJ to show some examples.

1. Introduction

XPath is a W3C recommendation. It stands for XML Path Language and uses “path like” syntax to identify and navigate nodes in an XML document. XPath is a major element in the XSLT standard. XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the path expressions you use with traditional computer file systems.
XPath includes over 200 built-in functions. There are functions for string values, numeric values, booleans, date and time comparison, node manipulation, sequence manipulation, and much more. Today XPath expressions can also be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

2. XPath Like Operator

There are various functions available which can be used to mimic the same behaviour as the ‘like’ operator. Below we will see some of those:

2.1 starts-with(string1, string2)

The starts-with checks whether the first string starts with the second string and returns true or false. The first parameter of the starts-with XPath function is used to specify the source node or string against which the comparison is to be executed. The second parameter is the pattern string that specifies the character or character sequence that is to be used in the comparison. It is important to remember that the pattern string that is supplied as the second parameter of the starts-with function is case sensitive.
For example starts-with(Hello, World) will return false while starts-with(Hello World, Hello) will return true.

2.2 contains(string1, string2)

The contains() function determines whether the first argument string contains the second argument string and returns boolean true or false. If an argument is not of type string, it is first converted to a string using the string() function and then the result of that conversion is evaluated. Please note that this function is also case-sensitive.
XPath 2.0 also supports the regular expressions which can be used for the queries involving the like operator.

2.3 Pattern matching

The three functions described below make use of a regular expression syntax for pattern matching:fn:matches: Returns an xs:boolean value that indicates whether the value of the first argument is matched by the regular expression that is the value of the second argument.fn:replace: Returns the value of the first argument with every substring matched by the regular expression that is the value of the second argument replaced by the replacement string that is the value of the third argument.fn:tokenize: Returns a sequence of one or more xs:strings whose values are substrings of the value of the first argument separated by substrings that match the regular expression that is the value of the second argument.
We can use the matches() function for our case.

3. Example

In this section we will show the working example of the things we have discussed in the previous section. For this example we will make use of IDEA IntelliJ but you can use any other IDE of your choice.

Open IntelliJ and click on File=>New=>Project. Choose Java and click Next. Click Next on the next pop-up. Give the name of the project and the location and click Finish.

Figure 1. Java Project

IntelliJ will create a basic structure for your project.

Now we will see how to create a new java package. To create a new package right click on the ‘src’ folder and choose New=>Package.

Figure 2. New Package

Give the package name (com.javacodegeeks) and click OK. IntelliJ will create a package for you. Now right click on the package and choose New=>Java Class. Give the class name and click OK. IntelliJ will create a class for you in the specified package.

For simplicity we will create an XML file at the same location where the java file exist. Ideally you should keep your static files in different location. To create an XML file right click on the package and choose New => File. Give the file name and click OK. We will create some test entries as below:

A DocumentBuilderFactory defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents. Then we will get the DocumentBuilder object by calling the newDocumentBuilder() method on the factory object:

builder = builderFactory.newDocumentBuilder();

Once an instance of this class is obtained, XML can be parsed from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.
Now we will parse the xml file by calling the parse() method on the builder object and passing the xml path to it:

4. Conclusion

In this article we discussed about XPath and how we can use various functions available to perform a ‘like’ query. We also discussed about Pattern matching and how to use the available functions to perform actions on the data. We also discussed about creating a java project using IntelliJ and writing the code to parse the xml and to perform XPath queries.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.