I answered questions about the interview quiz, and the question was how I would take screenshots of the screen. That is, by selecting content from a web page, assuming that you do not have a more structured way to directly request information (for example, a web service).
My solution was to use an XQuery expression. The expression was quite long because the content I needed was pretty deep in the HTML hierarchy. I had to find my ancestors in an honest way before I discovered an element with an id attribute. For example, clearing an Amazon.com page for product sizes is as follows:
//a[@id="productDetails"] /following-sibling::table //h2[contains(child::text(), "Product Details")] /following-sibling::div //li /b[contains(child::text(), "Product Dimensions:")] /following-sibling::text()
This is a pretty nasty expression, but why does Amazon provide a web service API. In any case, this is just one example. The question is not about Amazon, but about screen scripting.
The interviewer did not like my decision. He thought it was fragile because redesigning an Amazon page might require rewriting an XQuery expression. Debugging an XQuery expression that does not match anything on the page to which it is applied is complex.
I did not agree with his statements, but I did not think that his decision was any improvement: he thought it was better to use regular expression , and search for content and markup near the delivery weight, for example, using Perl:
$html =~ m{<li>\s*<b>\s*Product Dimensions:\s*</b>\s*(.*?)</li>}s;
My counterargument was that it is also susceptible to Amazon changing its HTML code. They can write HTML tags in capitals ( <LI> ) or add CSS attributes or change <b> to <span> or change the label "Product Dimensions:" to "Dimensions:" or many other kinds of changes. My point was that regular expressions do not eliminate the flaws that it caused in my XQuery solution.
But, in addition, regular expressions can find false positives if you do not add enough context to the expression. It can also inadvertently map content that is inside a comment, or an attribute string, or a CDATA section.
My question is: what technology do you use to clean the screen? Why did you choose this solution? Are there any good reasons to use it? Or never use another? Is there a third choice besides the ones I showed above?
PS: For the sake of argument, suppose the web service API does not exist or another more direct way to get the desired content.