Cover Image for XPath Comparison
118 views

XPath Comparison

In XPath, comparison operators are used to evaluate conditions and compare values within XML or HTML documents. They allow you to create expressions that filter nodes based on specific criteria. XPath provides several comparison operators to perform these evaluations. Here are the most commonly used comparison operators in XPath:

  1. = (Equal to):
  • Example: //book[@category='fiction']
  • Selects all <book> elements that have a “category” attribute with the value “fiction.”
  1. != (Not equal to):
  • Example: //book[@category!='fiction']
  • Selects all <book> elements that do not have a “category” attribute with the value “fiction.”
  1. < (Less than):
  • Example: //book[price<20]
  • Selects all <book> elements that have a child element named “price” with a value less than 20.
  1. > (Greater than):
  • Example: //book[price>20]
  • Selects all <book> elements that have a child element named “price” with a value greater than 20.
  1. <= (Less than or equal to):
  • Example: //book[price<=20]
  • Selects all <book> elements that have a child element named “price” with a value less than or equal to 20.
  1. >= (Greater than or equal to):
  • Example: //book[price>=20]
  • Selects all <book> elements that have a child element named “price” with a value greater than or equal to 20.
  1. contains():
  • Example: //book[contains(title, 'Harry Potter')]
  • Selects all <book> elements that have a child element named “title” containing the text “Harry Potter.”
  1. starts-with():
  • Example: //book[starts-with(title, 'The')]
  • Selects all <book> elements that have a child element named “title” starting with the text “The.”
  1. ends-with():
  • Example: //book[ends-with(title, 'Hobbit')]
  • Selects all <book> elements that have a child element named “title” ending with the text “Hobbit.”

XPath comparison operators are used in conjunction with element names, attributes, and values to create expressions that filter and select specific nodes within XML or HTML documents. These expressions allow you to retrieve relevant data from the document based on your specific criteria.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS