XPath
XDV uses XPath 1.0 to specify elements on which the transformation should happen. If you donʼt feel comfortable with this, wait until you find out how easy it is to get XPath from a CSS selector. If after this you will still don't feel comfortable, you might want to check out Deliverance which allows both XPath and basic CSS selectors for specifying elements. Here are some tools you can use.
Firebug
One of the easiest ways of getting XPath is to use the Firebug extension for Firefox.
Pros:
- works in offline mode;
- is already your main (I suppose) debugging tool.
Cons:
- you need to have two different tabs running your Plone site - one for the site without transformation (by default from 127.0.0.1:8080) and another one for the transformed site with the theme applied (by default localhost:8080);
- the quality and complexity of XPath returned in this case might be not good enough for your needs.
- Firebug gives XPath for only one particular element that you have clicked. This means it can't give XPath for more than one element and can't make it recursive. So, for instance, that doesn't allow you to get XPath for all list items in unordered lists.
CSS2XPath
Being a CSS person, I prefer getting XPath from CSS selectors. I can construct CSS selectors fully understanding what they are supposed to return. So for me getting XPath from a CSS selector is the most advanced, flexible and clear way. Ian Bicking wrote an online tool that does exactly that - it converts a CSS selector into XPath - http://css2xpath.appspot.com/.
Pros:
- intuitively clear;
- you work with what you really know;
- the resulting XPath is usually much better than the one from Firebug.
Cons:
- it is online.
Python
You can get the same results as Ianʼs tool gives you but in offline mode. For this you will need to have lxml for Python installed. Sounds scary, right? But the good news is - if you have an instance running with collective.xdv you already have lxml for your python installed. In your buildoutʼs folder you just need to run
./bin/zopepy
You will get a Python prompt that will make all products installed for your instance including lxml (since it is a dependency for collective.xdv) available for you. So we can do the following:
>>> from lxml.cssselect import css_to_xpath
>>> css_to_xpath("div + p")
"descendant-or-self::div/following-sibling::*[name() = 'p' and (position() = 1)]"
Now you can use the returned XPath for specifying elements in your rules.xml.
