开发者

Xpath to select commented code

开发者 https://www.devze.com 2023-01-19 08:26 出处:网络
I have this xpath query: descendant-or-self::link and this html code: <html xmlns=\"http://www.w3.org/1999/xhtml\">

I have this xpath query:

descendant-or-self::link

and this html code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/elements.css">
<title>Page</title>
<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="css/ie6.css" />
<![endif]-->
</head>
etc...

What do I need to add to开发者_JS百科 this query to make it also match the commented <link>.

This is a continuation of this question: PHP DomDocument alter conditional comments


In XPath 1.0, with proper XHTML:

/xhtml:html/xhtml:head/xhtml:link|
/xhtml:html/xhtml:head/comment()[contains(.,'link')]

With a DOM provider that it doesn't handle namespaces:

/html/head/link|
/html/head/comment()[contains(.,'link')]

Also:

/html/head/node()[self::link or self::comment()[contains(.,'link')]]


Use the union operator "|":

descendant-or-self::link|/*/comment()

This will return the text of the comment, which cannot be used for further parsing even though it contains markup-like text. It's just a string so you'll have to treat it like one.

0

精彩评论

暂无评论...
验证码 换一张
取 消