How to get H1,H2,H3,… using a single xpath expression

Use:

/html/body/*[self::h1 or self::h2 or self::h3]/text()

The following expression is incorrect:

//html/body/*[local-name() = "h1"  
           or local-name() = "h2"  
           or local-name() = "h3"]/text()  

because it may select text nodes that are children of unwanted:h1, different:h2, someWeirdNamespace:h3.

Another recommendation: Always avoid using // when the structure of the XML document is statically known. Using // most often results in significant inefficiencies because it causes the complete document (sub)tree roted in the context node to be traversed.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)