kyleburton / clj-xpath

Simplified XPath Library for Clojure

This URL has Read+Write access

commit  f7d92a5cdbe81757d91634cf690814345022932f
tree    10c1af8c3e7855b2a82eb6599397728b22bbc1f6
parent  67f830263dda2873d3518c09624223c28348e930
name age message
file .gitignore Loading commit data...
file README.textile
directory bin/
directory docs/
file pom.xml
directory src/
README.textile

clj-xpath

Description

Simplified XPath Library for Clojure. XML Parsers and an XPath implementation now comes with Java 6, though using the api directly can be verbose and confusing. This library provides a thin layer around basic parsing and XPath interaction for common use cases. I have personally found the ability to interactively tweak my xpath expressions to be a great productivity boost – even using this library only for that has helped me in my learning of and using xpath. I hope you find it useful and would love to hear your feedback and suggestions.

Trying It Out

The build (see ‘Building’ for instructions on building this software) creates a REPL script (bin/repl) that you can use to quickly try the library out:


kyle@indigo64 ~/personal/projects/clj-xpath[master]$ bin/repl
Clojure 1.0.0
user=> (use 'com.github.kyleburton.clj-xpath)
nil
user=> (use 'clojure.contrib.duck-streams)
nil
user=> (def xdoc (slurp* "http://github.com/kyleburton/clj-xpath/raw/master/pom.xml"))
#'user/xdoc
user=> ($x:tag "/*" xdoc)
:project
user=> ($x:text* "/project/developers/developer/name" xdoc)
("Kyle Burton")
user=> (doseq [node ($x "/project/dependencies/dependency" xdoc)]
  (prn (format "%s %s %s"
               ($x:text "./groupId"    node)
               ($x:text "./artifactId" node)
               ($x:text "./version"    node))))
"org.clojure clojure 1.0.0"
"org.clojure clojure-contrib 1.0.0"
"commons-math commons-math 1.2"
"log4j log4j 1.2.14"
"commons-logging commons-logging 1.1.1"
"junit junit 3.8.1"
nil
user=> (System/exit 0)
kyle@indigo64 ~/personal/projects/clj-xpath[master]$

Usage

The main functions in the library are $x and those named with a prefix of $x: (eg: $x:text). The rationale for choosing $x as a name was based on the FireBug xpath function and it being a short and uncommon name. These xpath functions all take the xpath expression to be executed and an XML document. They attempt to be flexible with respect to the form of the XML document may represent. If it is a string it is treated as XML, if a byte array it is used directly, if already a Document or Node (from org.w3c.dom) they are used as-is.

There are two forms of many of the functions, with one set ending with a star ‘*’. The non-suffixed forms will perform their operation on a single result node, raising an exception if there isn’t exactly one result from applying the xpath expression to the XML document. The ‘*’ suffixed forms return all of the matched nodes for further processing.

$x function returns a map containing the XML tag (as a symbol), dom Node, the text (as a string), and a map of the attributes where the keys have been converted into symbols and the values remain Strings.


(ns example
  (use [com.github.kyleburton.clj-xpath :only [$x $x:tag $x:text $x:attrs $x:attrs* $x:node]]))


(def *some-xml*
     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<books>
  <book title=\"Some Guide To XML\">
    <author>
      <name>P.T. Xarnum</name>
      <email>pt@x.m.l</email>
    </author>
    <description>
      Simply the most comprehensive XML Book on the market today.
    </description>
  </book>
  <book title=\"Some Guide To Functional Programming\">
    <author>
      <name>S. Hawking</name>
      <email>universe@cambridge.ed.u</email>
    </author>
    <description>
      This book is too smart for you, try 'Head first Quantum Mechanics for Dummies' instead.
    </description>
  </book>
</books>")


;; get the top level tag:
(prn ($x:tag "/*" *some-xml*))
;; :books

;; find all :book nodes, pull the title from the attributes:
(prn (map #(-> % :attrs :title) ($x "//book" *some-xml*)))
;; ("Some Guide To XML" "Some Guide To Functional Programming")

;; same result using the $x:attrs* function:
(prn ($x:attrs* "//book" *some-xml* :title))
;; ("Some Guide To XML" "Some Guide To Functional Programming")

;; first select the :book element who's title has 'XML' in it
;; from that node, get and print the author's name (text content):
(prn ($x:text "./author/name"
              ($x:node "//book[contains(@title,'XML')]" *some-xml*)))
;; "P.T. Xarnum"

API Reference

($x xpexpr doc) => (map [...])

This is the main xpath application function. It takes an xpath expression (as a string) and a document and returns a sequence of matched elements as maps. The map contains the node’s tag, attributes, text content and the dom Node itself.


  { :tag    :tag-name
    :attrs  { :a "map" :of "the attributes" }
    :text   "the body content of the node and it's children"
    :node   org.w3c.dom.Node }

($x:tag* xpexpr doc) => [:tag-name ...]
($x:text* xpexpr doc) => ["the content" ..]
($x:attrs* xpexpr doc) => [{:the "attrs" ...} ...]
($x:node* xpexpr doc) => [org.w3c.dom.Node ...]

These functions apply the xpath expression to the given document and return a sequence of the requested property.

($x:tag xpexpr doc) => :tag-name
($x:text xpexpr doc) => "the content"
($x:attrs xpexpr doc) => {:the "attrs" ...}
($x:node xpexpr doc) => org.w3c.dom.Node

These functions return the requested property from the single result of executing the xpath expression. If the xpath expression identifies less than 1 result or more than 1 result in the given document, an exception is raised.

(xml->doc doc) => Document

This function takes xml that is of one of the following types and returns a Document: String, byte array or org.w3c.dom.Document. In cases of repeated usage of the document (eg: executing multiple xpath expressions against the same document) this will improve performance.

($x:compile xpexpr) => javax.xml.xpath.XPathExpression

Pre-compiles the xpath expression. In cases of repeated execution of the xpath expression this will improve performance.

Parsing and XPath Compilation

The $x and related functions support Strings, and in many cases, other convenient types for these arguments. In all cases where it expects an XML Document it can be given a String, a byte array or a Document. Where an xpath expression is expected it will take either a String or a pre-compiled XPathExpression. The act of parsing an XML document or compiling an xpath expression is an expensive activity. With this flexibility, clj-xpath supports the convenience of in-line usage (with String data), as well as pre-parsed and pre-compiled instances for better performance.


  (let [expr (xp:compile "/*")
        doc  (xml->doc "<authors><author><name>P.T. Xarnum</name></author></authors>")]
    ($x:tag expr doc))

Building

clj-xpath is built with maven at this time. It requires both clojure (1.0) and clojure-contrib (1.0) to be installed in an accessible maven repository. The software includes a script to install them into your local (~/.m2) repository for you


  kyle@indigo64 ~/personal/projects/clj-xpath[master]$ bash bin/maven-bootstrap.sh
  kyle@indigo64 ~/personal/projects/clj-xpath[master]$ mvn install

After running mvn install, clj-xpath-1.0.jar will be available for you to utilize in the target directory. To build a stand-alone jar file for clj-xpath which will contain its own code as well as clojure and clojure-contrib, run the assembly plugin:


  kyle@indigo64 ~/personal/projects/clj-xpath[master]$ mvn assembly:assembly

This will create the clj-xpath-1.0-jar-with-dependencies.jar jar in the target directory, which will contain all of the required dependencies for using the clj-xpath library (it will include clojure and clojure-contrib).

Maven Coordinates For clj-xpath


    <dependency>
      <groupId>com.github.kyleburton</groupId>
      <artifactId>clj-xpath</artifactId>
      <version>1.0.0</version>
    </dependency>

Emacs and Slime

The build creates an Emacs-lisp file in the bin directory which you can pull into your running Emacs to add it to the list of lisp implementations for SLIME.


(add-to-list 'slime-lisp-implementations
             '(clj-xpath ("/Users/kburton/personal/projects/clj-xpath/target/../bin/repl")
                        :init swank-clojure-init
                        :init-function krb-swank-clojure-init) t)

Authors

  • Kyle Burton <kyle.burton@gmail.com>
  • Trotter Cashion <cashion@gmail.com>