Test XPath Expressions Online

Use the XPath Tester to find nodes on a public webpage with an XPath expression. Enter a URL and an expression such as //img, //main//a, or //*[@data-id]. The tool displays the matching nodes and generates C# code for the same query.

  1. Enter the URL of the webpage you want to inspect.
  2. Enter an XPath expression that identifies the required nodes.
  3. Click Find, review the matches, and copy the result or generated C# code.


Evaluate an XPath Query on HTML in C#

Aspose.HTML for .NET evaluates XPath expressions against the document DOM. This example loads the URL currently shown in the widget, calls Evaluate() , and processes the returned IXPathResult. Change the URL or expression in the widget and this server-rendered C# example updates automatically. The context node defines where evaluation starts, while XPathResultType specifies the requested result type.

XPath Query in C#

using System;
using Aspose.Html;
using Aspose.Html.Dom;
using Aspose.Html.Dom.XPath;

// Create an HTML document
string url = "https://www.aspose.com/";
using HTMLDocument document = new HTMLDocument(url);

// Evaluate the XPath expression
IXPathResult result = document.Evaluate("//a", document, null, XPathResultType.Any, null);

// Process the XPath result
switch (result.ResultType)
{
    case XPathResultType.Number:
        Console.WriteLine(result.NumberValue);
        break;
    case XPathResultType.String:
        Console.WriteLine(result.StringValue);
        break;
    case XPathResultType.Boolean:
        Console.WriteLine(result.BooleanValue);
        break;
    default:
        Node node;
        while ((node = result.IterateNext()) != null)
        {
            if (node is Element element)
            {
                Console.WriteLine(element.OuterHTML);
            }
            else
            {
                Console.WriteLine(node.TextContent);
            }
        }
        break;
}

Common XPath Query Patterns

XPath can select elements by name, attribute, position, hierarchy, or text content:

  • //img selects every <img> element in the document.
  • //main//a selects links at any depth inside <main>.
  • //*[@data-id] selects elements that have a data-id attribute.
  • //ul/li[1] selects the first <li> child of each <ul>.
  • //p[contains(normalize-space(.), 'price')] selects paragraphs whose normalized text contains price.

For concise class, ID, attribute, or element queries that do not need text conditions or XPath axes, the CSS Selector Tester is often easier to read.


XPath Evaluation Workflow in C#

  1. Create an HTMLDocument from a local HTML file or URL.
  2. Define the XPath expression and choose the appropriate context node.
  3. Call Evaluate() with the expression, context, namespace resolver when required, and the expected XPathResultType .
  4. Read the returned value or iterate matching nodes with IterateNext() .

When an expression uses namespace prefixes, provide a resolver that maps those prefixes to namespace URIs. For ordinary HTML queries without prefixes, the resolver can be null.


XPath Documentation and API Reference

Use these resources for XPath evaluation, HTML navigation, and XML-specific examples:


Get Started with Aspose.HTML for .NET API

Install Aspose.HTML for .NET from NuGet with the .NET CLI:

dotnet add package Aspose.HTML

You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the installation guide for setup details, or request a temporary license to evaluate the API without evaluation limitations.


FAQ

1. What is an XPath expression?

An XPath expression is a query that selects nodes or calculates values from an HTML or XML document tree. It can use paths, axes, predicates, and functions to describe the required result.

2. Can XPath query HTML as well as XML?

Yes. XPath operates on a DOM tree and can select nodes in HTML or XML documents. The online tester is intended for public HTML webpages; use the documented C# XML workflow when namespace handling or XML-specific loading is required.

3. Why does an XPath expression return no nodes?

Verify the node names, predicates, context node, and loaded document structure. For XML or namespace-qualified content, also supply a namespace resolver that maps every prefix used in the expression.

4. When should I use XPath instead of a CSS selector?

Use XPath when the query must match text, navigate to ancestors or siblings, or apply predicates that are awkward in CSS. Use a CSS selector for familiar class, ID, attribute, and descendant queries.

Related HTML Selection and Editing Workflows

Continue with CSS selectors, DOM editing, table extraction, or image extraction in C#.