Translations of this page:

Chapter 11: XPath containers

HWZT chapter 11 (XPath containers):

Again, a few changes since HWZT, and again, using its first Scaffold example as an example:

  1. Close any running Scaffold 2.0 instances.
  2. Ensure the first sample page is open in your browser and has focus.
  3. Open Scaffold 2.0 from the Firefox main menu with Tools>Scaffold. This should popup dialog=“Zotero Scaffold”.
  4. If you are not already in tab=Metadata, select that. Enter some text in the Label and Creator fields.
  5. The URI of the sample page has changed since HWZT, so you will need to enter Target=
    http://niche-canada.org/member-projects/zotero-guide/
  6. Hit button=“Test Regex”. You should get a result, in the “Test Frame” on the right of the tab, similar to that described in HWZT.
  7. Instead of
    Click on the "Detect Code" tab

    , click on tab=Code.

  8. In that tab enter
    function detectWeb(doc, url) {
      var namespace = doc.documentElement.namespaceURI;
      var nsResolver = namespace ? function(prefix) {
        if (prefix == "x" ) return namespace; else return null;
        } : null;
      var myXPath = '//td[1]';
      var myXPathObject = 
        doc.evaluate(myXPath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
      Zotero.debug(myXPathObject);
    }
  9. Click on icon=“Run detectWeb” (the eye): you should get results like
    12:00:00 Title:

The code for the second complete Scaffold example (from “Example 11.10”) is similarly

function detectWeb(doc, url) {
  var namespace = doc.documentElement.namespaceURI;
  var nsResolver = namespace ? function(prefix) {
    if (prefix == "x" ) return namespace; else return null;
    } : null;
  var myXPath = '//div[@id="Content"]/div/table[@class="Bibrec"]/tbody/tr/td[1][@class="Label"]';
  var myXPathObject = doc.evaluate(myXPath, doc, nsResolver, XPathResult.ANY_TYPE, null);
  var items = new Object();
  var headers;
  while (headers = myXPathObject.iterateNext()) {
    items[headers.textContent]='';
  }
  Zotero.debug(items);
}

Click on icon=“Run detectWeb” (the eye): you should get results like

12:00:00 'Title:' => ""
  'PrincipalAuthor:' => ""
  'Imprint:' => ""
  'Subjects:' => ""
  '' => ""
  'ISBN-10:' => ""
  'Collection:' => ""
  'Pages:' => ""

Next: Chapter 12: Regular Expressions

dev/how_to_write_a_zotero_translator_2nd_edition/chapter_11.txt · Last modified: 2011/04/03 16:16 by debweb