Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
dev:technologies [2017/11/18 16:29] – Remove Firebug & replace with "Inspect element". adamsmith | dev:technologies [2017/12/07 13:42] – bwiernik | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | + | Three commonly-used technologies |
- | in the process of updating the documentation for | + | |
- | <a href=" | + | |
- | may be outdated in the meantime. Thanks | + | |
+ | ==== CSS Selectors ==== | ||
+ | A comprehensive and accessible resource is available from W3Schools: | ||
+ | https:// | ||
- | A very brief introduction to some commonly used technologies used in [[dev:translators|translator development]]. | + | The Mozilla Development Network (MDN) also provides a CSS Selector resource:\\ |
+ | https:// | ||
- | ====== XPath ====== | + | ==== XPath ==== |
- | XPath provides a way to refer to specific parts of HTML or XML documents. It's usually the best way to extract data from webpages when writing a translator. | + | |
- | An XPath expression | + | A tutorial |
+ | https:// | ||
- | * ''/'' | + | ==== Regex ==== |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * Plus much more. See the [[http:// | + | |
- | The best introduction to XPath for use in translators | + | There are many high-quality regex tutorials available online. A comprehensive guide is provided all in one page at:\\ |
+ | https://github.com/zeeshanu/learn-regex/ | ||
- | === Examples === | + | Regex 101 also provides |
- | <code html> | + | http://regex101.com/ |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | <tr class=" | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | For the sample document above, these expressions would refer to... | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | + | ||
- | === In Translators === | + | |
- | When writing Zotero translators, | + | |
- | <code javascript> | + | |
- | var xpath = '// | + | |
- | var tableRows = doc.evaluate(xpath, | + | |
- | </ | + | |
- | Then iterate through the rows using '' | + | |
- | <code javascript> | + | |
- | while (var row = tableRows.iterateNext()) { | + | |
- | // do something with each row | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Starting with Zotero 2.1.9, there are two new utility functions for XPath, '' | + | |
- | + | ||
- | <code javascript> | + | |
- | var xpath = '// | + | |
- | var tableRows = Zotero.Utilities.xpath(doc, | + | |
- | for (var row in tableRows) { | + | |
- | // do something with each row | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | + | ||
- | ==== Using Firefox Inspect Element to design XPath or CSS Selector expressions ==== | + | |
- | In Firefox, you can right click on an element and select " | + | |
- | + | ||
- | + | ||
- | Then we can paste the XPath expression into the editor (i.e., the code pane of [[dev: | + | |
- | + | ||
- | < | + | |
- | / | + | |
- | </ | + | |
- | + | ||
- | XPath like this, however, is very fragile. It's better to latch onto some identifiers and make an expression relative to them: | + | |
- | < | + | |
- | // | + | |
- | </ | + | |
- | + | ||
- | and even better to rely on specific attributes in a much shorter Xpath such as | + | |
- | < | + | |
- | // | + | |
- | </ | + | |
- | ====== Regular Expressions ====== | + | |
- | Regular expressions are a way of matching and extracting pieces of text from a larger body of text. In translator development, | + | |
- | + | ||
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * ''?'' | + | |
- | + | ||
- | For a complete description of how regular expressions are used in JavaScript, with examples of their use, see Mozilla' | + | |
- | + | ||
- | There are many regular expressions guides on the web, and entire books on the topic. While you can and should consult these guides, keep in mind that the details of how they work will depend a little on the environment they' | + |