<p>I am creating a tool that will check dynamically generated XHTML and validate it against expected contents.</p>

<p>I need to confirm the structure is correct and that specific attributes exist/match. There may be other attributes which I'm not interested in, so a direct string comparison is not suitable.</p>

<p>One way of validating this is with XPath, and I have implemented this already, but I would also like something less verbose - I want to be able to use CSS Selectors, like I can with jQuery, but on the server - within CFML code - as opposed to on the client.</p>

<p><strong>Is there a CFML or Java library that allows me to use CSS Selectors against an XHTML string?</strong></p>

I don't know of a Java library itself, but there is a Ruby library called
Hpricot
that does exactly what you're looking for. In conjunction with the Ruby implementation on the Java platform,
JRuby
, it should be relatively straightforward to call Ruby methods from your Java code (using BSF,
JSR-222 Scripting APIs
, or
an internal API
).
Are you using Coldfusion 8? Coldfusion 8, being based on Java 6, supports JSR-222 Scripting APIs "javax.scripting".
Take a look at
this blog entry on embedding PHP within CFML
. You should be able to do
the same with Ruby
. There is ZIP file example code linked from this blog posting, and if you crack open the CFML, you'll see a good example of embedding Ruby within CFML.
Although it might take a bit of work to make all the pieces work together, but with a bit of investment, it should give you the robust parsing/CSS selector querying that you're looking for.