Skip to content

XPath is a powerful language for selecting nodes in an XML or HTML document. JavaScript provides built-in support for XPath through the document.evaluate() method, which can be used to evaluate an XPath expression and return the matching nodes in the document

License

Notifications You must be signed in to change notification settings

VolkanSah/JavaScript-XPath-Tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Using XPath with JavaScript

XPath is a powerful language for selecting nodes in an XML or HTML document. JavaScript provides built-in support for XPath through the document.evaluate() method, which can be used to evaluate an XPath expression and return the matching nodes in the document.

Usage

Here's an example of how to use XPath with JavaScript to select and manipulate the content of an HTML document:

<!DOCTYPE html>
<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
  </body>
</html>
// Get the first <li> element in the document
var listItem = document.evaluate("//li[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

// Change the text of the <li> element
listItem.textContent = "New item";

This code selects the first

  • element in the document using the XPath expression //li[1], and then changes its text content to "New item".

    The document.evaluate() method takes several arguments:

    • expression: The XPath expression to evaluate.
    • contextNode: The node from which to start the search. In this case, we want to search the entire document, so we use document.
    • namespaceResolver: A function that resolves namespaces used in the XPath expression. We're not using any namespaces, so we use null.
    • resultType: The type of result to return. In this case, we want to return the first matching node, so we use XPathResult.FIRST_ORDERED_NODE_TYPE.
    • result: An existing XPathResult object to reuse. We're not reusing an existing object, so we use null.

    Conclusion

    XPath is a powerful tool for selecting and manipulating nodes in an XML or HTML document, and JavaScript provides built-in support for XPath through the document.evaluate() method. With XPath, you can easily target specific nodes in a document and manipulate them to achieve your desired results.

    Thank you for your support!

    • If you appreciate my work, please consider becoming a 'Sponsor', giving a ⭐ to my projects, or following me.

    Credits

    License

    This project is licensed under the MIT - see the LICENSE file for details.

  • About

    XPath is a powerful language for selecting nodes in an XML or HTML document. JavaScript provides built-in support for XPath through the document.evaluate() method, which can be used to evaluate an XPath expression and return the matching nodes in the document

    Topics

    Resources

    License

    Stars

    Watchers

    Forks

    Sponsor this project