Programming

How to write an XPath query to match two attributes

27 September 2026 · 6 min read

How to write an XPath query to match two attributes

Navigating complex XML or HTML documents often requires precise selection criteria. When you’re tasked with extracting specific elements, relying solely on a single attribute might not be enough. This is where mastering how to write an XPath query to match two attributes becomes an invaluable skill. Understanding how to combine multiple conditions within your XPath expressions allows for highly targeted data extraction, streamlining tasks from web scraping to sophisticated data validation. This guide will delve into the fundamental principles and practical applications of crafting such powerful queries, ensuring you can pinpoint exactly what you need within any document structure.

Understanding XPath Basics for Attribute Selection

XPath, or XML Path Language, is a query language for selecting nodes from an XML document. It’s widely used in web scraping, XML parsing, and XSLT transformations to locate specific pieces of information. At its core, XPath treats an XML or HTML document as a tree structure, allowing you to navigate through elements, attributes, and text nodes with remarkable precision.

When working with attributes, XPath uses the @ symbol to target them. For instance, //div[@id] would select all <div> elements that possess an id attribute. To select a specific attribute value, you’d extend this by providing the value in quotes, such as //button[@class='submit-btn']. This basic understanding forms the foundation for more complex queries that involve multiple criteria. The ability to accurately perform attribute selection is crucial for efficient data extraction and automation processes, especially when dealing with dynamically generated content or large datasets.

Consider an HTML snippet like this: <a href="/products/item1" data-category="electronics">Item One</a>. To select this link based on its href attribute, you’d use //a[@href='/products/item1']. However, what if there are multiple links with the same href but you need the one specifically related to ’electronics’? This is where combining conditions becomes essential, allowing you to narrow down your selection with greater accuracy. For more in-depth knowledge on XPath syntax and its various applications, you can consult the W3C XPath 1.0 Specification, which defines the language’s core functionalities.

The Logic of Matching Multiple Attributes with ‘and’

To accurately write an XPath query to match two attributes, the logical and operator is your primary tool. This operator allows you to combine multiple conditions, ensuring that an element is selected only if all specified conditions are true. The syntax is straightforward: you place the and keyword between your attribute conditions within the square brackets [] that define a predicate.

For example, if you have an element like <input type="text" name="username"> and you want to select it specifically because it’s a text input and its name is ‘username’, your XPath query would look like this: //input[@type='text' and @name='username']. This expression will only return the <input> element that satisfies both attribute conditions simultaneously. This precision is vital in web scraping, where elements might share common attributes but differ in another, making a simple single-attribute query insufficient.

To match elements that possess two specific attributes, you use the and boolean operator within your XPath predicate. This ensures that an element is only selected if both attribute conditions are met, providing highly accurate and targeted results for data extraction and document navigation.

It’s important to remember that the and operator applies strict logic. If even one of the conditions linked by and is false, the entire predicate becomes false, and the element will not be selected. This strictness is a feature, not a bug, as it guarantees that your XPath expression is incredibly specific. For instance, if you’re trying to extract product details from an e-commerce site, you might look for a <div> with @class='product-card' and @data-id='12345' to ensure you’re getting the exact product card you intend. This approach minimizes false positives and improves the reliability of your automated processes.

Practical Examples and Use Cases for Dual Attribute Matching

Let’s dive into some practical scenarios where knowing how to write an XPath query to match two attributes proves incredibly useful. Imagine you’re scraping a job board. You might encounter job listings represented by <div> elements, where some are ‘featured’ and others are ‘standard’. Each might also have a unique ‘job-id’.

Consider this HTML structure:

<div class="job-listing" data-type="featured" data-id="101">...</div> <div class="job-listing" data-type="standard" data-id="102">...</div> <div class="job-listing" data-type="featured" data-id="103">...</div> 

If you wanted to select only the featured job listing with a specific ID, say ‘103’, your XPath would be: //div[@class='job-listing' and @data-type='featured' and @data-id='103']. This query precisely targets the third <div> in our example, demonstrating the power of combining multiple conditions. This level of specificity is often required in complex web scraping projects to ensure accurate data extraction.

Another common use case is validating XML documents. Suppose you have an XML file describing books, and each book node has an id and a status attribute. You might want to find all books that are both ‘available’ and have a specific ‘id’.

<book id="B001" status="available">...</book> <book id="B002" status="checked-out">...</book> <book id="B003" status="available">...</book> 

To select the book with id="B003" that is also status="available", the XPath query would be: //book[@id='B003' and @status='available']. These examples highlight how Boolean operators in XPath allow for fine-grained control over element selection, making your queries robust and efficient for various data manipulation tasks.

Infographic here
Advanced Techniques and Best Practices for Robust Queries ---------------------------------------------------------

While the and operator is fundamental for matching two attributes, XPath offers more advanced techniques that can make your queries even more robust and adaptable. Sometimes, you might need to select an element if it matches one attribute OR another, for which the or operator is used. For instance, //element[@attr1='value1' or @attr2='value2'] selects elements matching either condition. However, for matching multiple attributes simultaneously, and remains the go-to. Combining these boolean operators with other XPath axes and functions can lead to incredibly powerful XPath expressions.

When crafting complex XPath queries, consider the following best practices:

  • Be Specific But Not Overly Restrictive: Aim for enough specificity to uniquely identify the element, but avoid tying your query to too many static attributes if some might change. A good balance leads to resilient queries.
  • Use Relative Paths: Start with // (anywhere in the document) or specific parent elements to make your queries less brittle to minor structural changes.
  • Test Thoroughly: Always test your XPath queries against the target document. Tools like browser developer consoles (Elements tab, search with Ctrl+F/Cmd+F) or dedicated XPath testers are invaluable.

For situations where attribute values might be dynamic or partial, functions like contains(), starts-with(), or ends-with() can be used in conjunction with and. For example, //div[contains(@class, 'product') and starts-with(@id, 'item-')] would find a <div> whose class attribute contains “product” and whose ID attribute starts with “item-”. This flexibility is crucial for Question & Answer :

Following Question:

<div id="id-74385" class="guest clearfix" style="z-index: 999;"> 

Given above,

If I want a XPath expression with checks both id and class, can we do it w/ ‘and’ condition LIKE:

//div[@id='id-74385'] and div[@class='guest clearfix'] 

Is this correct way? My execution fails here… Please help!

//div[@id='...' and @class='...'] 

should do the trick. That’s selecting the div operators that have both attributes of the required value.

It’s worth using one of the online XPath testbeds to try stuff out.