(Difference between revisions)

Jump to: navigation, search

ChristopheDucamp (Talk | contribs)
(awaiting cleanup before translation)
Next diff →

Revision as of 07:53, 9 July 2006

Instructions et Stratégies pour Implémenter les Microformats

Cette page a démarré sur implementation-guidelines. La page originale a besoin de GROS MENAGE et de réorganisation avant de passer en traduction afin d'être utile pour y trouver des "instructions" pour les implémenteurs. A cette heure la page originale ne contient qu'un vague ensemble désordonné d'idées d'implémentations. Si vous êtes un implémenteur francophone, sentez-vous vraiment libre de sauter dans la page mentionnée au-dessus et d'aider au nettoyage. Merci d'avance -- Christophe .



   <title>Review of Some Object</title>
   <link href="" />
   <content type="xhtml">
    <div xmlns="">
     <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>



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. The following examples are in extreme summary form. It is suggested that you click through on the links for more explanation.

Stratégies Firefox/Mozilla

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')]",
// 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. = 'red';

A greasemonkey script that applies this strategy to and an individual linkblog republished from 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,
for (var i = 0; i < mc.snapshotLength; i++) {
    iNode = mc.snapshotItem(i);
    if (iNode.className.match(micromatch))

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(
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 = 'red';

Stratégies Internet Explorer

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.


Parsing Microformats can be as easy or as hard as you want it to be. Since many of the elementary building blocks are based on anchor tags, its possible possible to implement some fairly effective functionality without needing to mess around with the complexities of tag nesting and unknown class names. Simple rule: ignore anything you don't need.


voir : [[hcard-parsing-fr|hCard Parsing]

voir aussi

implementation-guidelines-fr was last modified: Wednesday, December 31st, 1969