XPath是否适合HTML5格式?

时间:2013-07-14 18:21:36

标签: html5 xpath

我们知道XPath是为XML解析而创建的。 HTML5怎么样,因为它不一定尊重XML规则?

3 个答案:

答案 0 :(得分:6)

有两种XML:Lexical XML和Parsed XML。 XPath适用于解析的XML表示,例如DOM或XDM。因此,您可以从Lexical HTML5创建解析XML,这样您就可以使用XPath查询HTML5。

答案 1 :(得分:2)

除非你有这样的警告,否则你可能会有这样的警告。

采用以下HTML:

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

现在大多数HTML解析器都会接受上面的含义:

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

现在,XPath /div/p[2]应该返回"And another..."但是,它同样可以解释为:

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

/div/p[2]未返回任何内容且XPath /div/p/p返回"And another..."

HTML不是XML,不需要格式良好,因此将HTML技术与HTML结合使用会导致特性异常。所以,只要你承认那些你应该没事。

答案 2 :(得分:1)

XPath用于查询DOM ,而不是解析标记。可以从HTML文档生成DOM,因此您可以使用XPath进行查询。