2

Is it possible to use xpath to find out if the following html document contains a:hover or has an element of size 12?

<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<style type="text/css">
a:link{
  color:#000;
}
a:hover{
  color:#333;
}
p{
  font-size:12px;
}
</style>
</head>
<body>
<a href="foo.com">some text here</a>
<p>some more text</p>
</body>

</html>

Any help appreciated,

Kay

2 Answers 2

1

You can't use XPath on invalid XML documents and this one is invalid. You can use, however HtmlAgilityPack or something like it, to parse HTML DOM аnd get content of a style tag. Then you must you some custom parsing for CSS or look for library that does that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the project link looks very useful - i can use //head//style to get the contents of the embedded css i think though. Yeah it looks like i might have to find a css parser after all. Many thanks :)
1

You can use xpath for not XML documents, and well CSS is not XML. So if your styling is not in HTML attributes you cannot look for style(unless browser translates CSS styles to DOM attributes, then it could work, maybe someone will say something about this). My suggestion would be to use regexp to extract what ids/classes/tags have desired CSS attributes and then use those in xpath. However for this most likely you will need multi line regex mode(not that this is big problem, just a note).

Well actually now that I think of it you can do content search in style tags using xpath, but well you still need to parse that content, and I don't think you will do this successfully without regexp, so i suggest skipping regex in CSS parsing step. Or just use xpath to collect script elements and parse their content after that, so that you would not need to parse all HTML.

1 Comment

I'm starting to think some form of parsing is needed as well since xpath is used to work with elements, attributes etc... bt can css really be called an attribute if it isn't inline css. The closest i've gotten so far is: //p[@style='font-size:12px']

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.