tags:

views:

28

answers:

2

Hi I have this xpath query:

descendant-or-self::link

and this html code:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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 this query to make it also match the commented <link>.

This is a continuation of this question: http://stackoverflow.com/questions/3879108/php-domdocument-alter-conditional-comments

+2  A: 

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.

alanstroop
A: 

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')]]
Alejandro
Ok, will I be able to treat it as a normal node or does it just return a text value?
Franky Chanyau
@Franky Chanyau: It returns two `link` elements and one comment node. Then you could manipulate the comment string value.
Alejandro