implementation-guidelines: Difference between revisions

From Microformats Wiki
Jump to navigation Jump to search
Line 56: Line 56:
</pre>
</pre>


A greasemonkey script that applies this strategy to [http://de.lirio.us de.lirio.us] and an [http://thecommunityengine.com/webcites/ individual linkblog republished from del.icio.us] is available [http://thecommunityengine.com/resources/xfolk-colorize.user.js] here with a tutorial on how the script works and how to modify it for other microformats [http://thecommunityengine.com/home/archives/2005/07/greasemonkey_mi.html here].  Alf Eaton uses this exact XPath strategy in an early [http://hublog.hubmed.org/archives/001149.html script] that he created for [[hreview]].
A greasemonkey script that applies this strategy to [http://de.lirio.us de.lirio.us] and an [http://thecommunityengine.com/webcites/ individual linkblog republished from del.icio.us] is available [http://thecommunityengine.com/resources/xfolk-colorize.user.js here] with a tutorial on how the script works and how to modify it for other microformats [http://thecommunityengine.com/home/archives/2005/07/greasemonkey_mi.html here].  Alf Eaton uses this exact XPath strategy in an early [http://hublog.hubmed.org/archives/001149.html script] that he created for [[hreview]].


However, as Alf Eaton also notes, the XPath-only strategy is subject to potential collisions where the class value that denotes the microformat's container element is contained in another class value.  For example 'xfolkentry' is contained in 'mixfolkentry', and the XPath expression would identify both as an [[xfolk]] entry.  While the danger of this particular collision occurring are slight, such may not be the case with other microformats.
However, as Alf Eaton also notes, the XPath-only strategy is subject to potential collisions where the class value that denotes the microformat's container element is contained in another class value.  For example 'xfolkentry' is contained in 'mixfolkentry', and the XPath expression would identify both as an [[xfolk]] entry.  While the danger of this particular collision occurring are slight, such may not be the case with other microformats.
Line 97: Line 97:
}
}
</pre>
</pre>


===Internet Explorer Strategies===
===Internet Explorer Strategies===


XPath-based strategies can be made to work sometimes in Internet Explorer with [http://glazkov.com/blog/archive/2004/04/06/168.aspx Dimitri Glazkov's javascript library] that harnesses IE's built-in XPath support for traversing HTML DOM.
XPath-based strategies can be made to work sometimes in Internet Explorer with [http://glazkov.com/blog/archive/2004/04/06/168.aspx Dimitri Glazkov's javascript library] that harnesses IE's built-in XPath support for traversing HTML DOM.

Revision as of 15:44, 31 July 2005

Guidelines and Strategies for Implementing Microformats

Publishers

  • Embedding in XHTML documents. See XMDP FAQ.
  • Microformats can be embedded in Atom entries:
 <entry>
   <title>Review of Some Object</title>
   <link href="http://microformats.org/wiki/" />
   <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
   <updated>20050730T1900-0700</updated>
   <content type="xhtml">
    <div xmlns="http://www.w3.org/1999/xhtml">
     <div class="hreview">
       <h3 class="summary"><span class="item fn">Some Object</span> Being Reviewed</h4>
       <p>Reviewer: <span class="reviewer fn">author name</span> - 
       <abbr class="dtreviewed" title="20050730T1900-0700">July 30, 2005</abbr></p>
       <blockquote class="description"><p>The review content</p></blockquote>
       <span><span class="rating">5</span> out of 5</span>
     </div>
    </div>
   </content>
 </entry>

Questions:

  • Should an XMDP profile be linked to the Atom entry?
  • Should the hreview class appear in the content container div?

Identification

There are several user agent identification strategies. Currently, Firefox provides strong support for XPath based discovery methods. Some of these can be adapted to Internet Explorer 6 with a small loss in efficiency and others not at all.

Firefox/Mozilla Strategies

The simplest method is just to use an XPath expression to identify the microformat. Here is an example that does just that:

var allDivs, thisDiv;

allDivs = document.evaluate(
    "//*[contains(@class, 'xfolkentry')]",
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);
		
// Just run through all elements matched by the XPATH expression and change
// the background color to an attractive red.
for (var i = 0; i < allDivs.snapshotLength; i++) {
    thisDiv = allDivs.snapshotItem(i);
    // do something here.  For example, change the background color to red.
    thisDiv.style.backgroundColor = 'red';
}

A greasemonkey script that applies this strategy to de.lirio.us and an individual linkblog republished from del.icio.us is available here with a tutorial on how the script works and how to modify it for other microformats here. Alf Eaton uses this exact XPath strategy in an early script that he created for hreview.

However, as Alf Eaton also notes, the XPath-only strategy is subject to potential collisions where the class value that denotes the microformat's container element is contained in another class value. For example 'xfolkentry' is contained in 'mixfolkentry', and the XPath expression would identify both as an xfolk entry. While the danger of this particular collision occurring are slight, such may not be the case with other microformats.

To avoid collisions in microformat container values, Alf proposes an amendment to the above strategy that works well for microformats where the container element is identified by a class value. Alf's strategy is contained in the following code pattern (that does not run as is):

var mf = 'hreview';

var micropath = "//*[contains(@class,'" + mf + "')]";
var micromatch = new RegExp('\\b' + mf + '\\b');

var mc = document.evaluate(micropath, document, null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < mc.snapshotLength; i++) {
    iNode = mc.snapshotItem(i);
    if (iNode.className.match(micromatch))
        doSomething();
}

Note that this solution introduces regular expression checking as a second test on the nodes isolated by the XPath expression. Modifying the first example to use this strategy in a bit of code that is runnable we get:

var allDivs, thisDiv;
var mf = 'xfolkentry';

var micropath = "//*[contains(@class,'" + mf + "')]";
var micromatch = new RegExp('\\b' + mf + '\\b');

allDivs = document.evaluate(
    micropath,
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);
		
for (var i = 0; i < allDivs.snapshotLength; i++) {
    thisDiv = allDivs.snapshotItem(i);
    // do something here.  For example, change the background color to red.
    if (iNode.className.match(micromatch)) // apply the second test
    thisDiv.style.backgroundColor = 'red';
}

Internet Explorer Strategies

XPath-based strategies can be made to work sometimes in Internet Explorer with Dimitri Glazkov's javascript library that harnesses IE's built-in XPath support for traversing HTML DOM.