     
 
     (if (not (empty? class-list)) 
-      (map #(format "%s[%s]" tag %) formatted-classes)
-      (list tag))))
+      (map #(cond (and (not is-first?)
+                       (not is-last?))             
+                  (format "%s[%s]" tag %)
+
+                  is-first?
+                  (format "%s[%s][1]" tag %)
+
+                  is-last?
+                  (format "%s[%s][last()]" tag %)) formatted-classes)
+      (cond (and (not is-first?)
+                 (not is-last?))
+            (list tag)
+
+            is-first?
+            (list (format "%s[1]" tag))
+
+            is-last?
+            (list (format "%s[last()]" tag))))))
 
 (defn tag-node->xpath
   [a-tagnode]

           (.getValue))
       (catch Exception e nil)))

(defn tag-id-class-node
  "Returns a list containing a tag's name, its id
- formatted slightly - and its classes - formatted slightly"

        silent-fail-attr  (fn [attr key] (try (.getValue
                                              (.getNamedItem attr key))
                                             (catch Exception e nil)))

        
        attributes        (.getAttributes a-w3c-node)

        is-first?         (is-first-child? a-w3c-node)

        is-last?          (is-last-child? a-w3c-node)]

    (list (.getNodeName a-w3c-node)               ; name
          (-> attributes
              (silent-fail-attr "id")
              format-attr)                        ; id
          (map
           format-attr
           (-> attributes
               (silent-fail-attr "class")
               (silent-fail-split #"\s+")))       ; class

          is-first?
          
          is-last?)))    

(defn tag-id-class->xpath
  "Args:
    a-tag-id-class : a tag, its id and a list of classes
   Returns:
    a component that fits in an xpath"
  [[tag id class-list is-first? is-last?]]
  (let [formatted-id      (format "contains(@id,'%s')" id)
        formatted-classes (map
                           #(format "contains(@class,'%s')" %)
