Is XPath suitable for HTML5 format?

We know XPath is designed for XML parsing. What about HTML5 as it does not necessarily follow XML rules?

+6
source share
3 answers

There are 2 XML: Lexical XML and Parsed XML. XPath works against XML representation with parse such as DOM or XDM . Therefore, you can create Parsed XML from Lexical HTML5, so you can query HTML5 using XPath.

+6
source

You can with great caution, which you probably shouldn't, if you have one too.

Take the following HTML:

<div> <p> Here is a paragraph <p> Here is another, is it inside the first, who knows? </div> 

Now most HTML parsers will agree that this means the following:

 <div> <p> Here is a paragraph </p> <p> And another, is it inside the first, who knows? </p> </div> 

Now XPath /div/p[2] should return "And another..." , however, it can be equally interpreted as:

 <div> <p> Here is a paragraph <p> And another, is it inside the first, who knows? </p> </p> </div> 

Where /div/p[2] returns nothing, and XPath /div/p/p returns "And another..." .

HTML is not XML and does not need a good education, so using XML technologies with HTML can lead to idiosyncrasies. So for now, you acknowledge that you should be fine.

+2
source

XPath is for querying the DOM, not parsing markup. The DOM can be generated from an HTML document, so you can request it using XPath.

+1
source

Source: https://habr.com/ru/post/949419/


All Articles