4

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 this query to make it also match the commented <link>.

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

2 Answers 2

4

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')]]
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, will I be able to treat it as a normal node or does it just return a text value?
@Franky Chanyau: It returns two link elements and one comment node. Then you could manipulate the comment string value.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.