Finding the First Node That Meets a Condition

Suppose you want to obtain from a collection the first node that meets a certain condition. For example, you want the first book whose author's last name is Bob. You can specify the following query:

(//book[author/last-name="Bob"])[position()=1]
               

            

When the XPath processor evaluates this expression, it creates a collection of book elements where the author's last name is Bob. The XPath processor then returns the first node in this collection.

The following two expressions appear to also return the first book whose author's last name is Bob, but they do not. Instead, these queries both return a book whose author's last name is Bob only if that book is the first book in the document.

//book[author/first-name="Bob"][position()=1]
               
//book[author/first-name="Bob" and position() = 1]
               

            

Convert EDIFACT to XML Schema

Convert EDIFACT to XML with Stylus Studio's XML Schema and legacy data integration tools.

Retail Companies That Use Stylus Studio

Stores and XML data can get very crowded. That's why retail companies use Stylus Studio to simplify their data. With the power of Stylus Studio these top retail companies are at the front of the line when it comes to XML!

Learn XSLT and XQuery Profiling

Stylus Studio's Profiler can help you evaluate the performance of your XQuery and XSLT. Automatically generated, customizable, easy-to-read HTML reports provide insight into performance bottlenecks.

XML Project

Stylus Studio's Project Window is an intuitive, visual feature for organizing files belonging to a project. It supports different file-type icons and nested folders; many helpful options are available from the context menu.

Stylus Most Wanted

 
Free Stylus Studio XML Training:
W3C Member