views:

49

answers:

2

Hi,

I need to build a component which would take a few XML documents in input and check the following kind of rules:

XML1:/bookstore/book[price>35.00] != null 
and (XML2:/city/name = 'Montreal'
     or XML3://customer[@language] contains 'en')

Basically my component should be able to:

  • substitute the XML tokens with the corresponding XML document(before colon)
  • apply xpath query on this XML document
  • check the xpath output against expected result ("=", "!=", "contains")
  • follow the basic syntax ("and", "or" and parentheses)
  • tell if the rule is true or false

Do you know any library which could help me? maybe JavaCC?

Thanks

A: 

For evaluating XPATHs I recommend JAXEN.

Jaxen is an open source XPath library written in Java. It is adaptable to many different object models, including DOM, XOM, dom4j, and JDOM. Is it also possible to write adapters that treat non-XML trees such as compiled Java byte code or Java beans as XML, thus enabling you to query these trees with XPath too.

The Java XPath API (Java 5 / javax.xml.xpath) is also an option, but I haven't tried it yet.

Bruno Rothgiesser
For the xpath part, I was considering using the JDK API. According to the following benchmark it seems faster than other libraries and I don't really have performance constraints anyway:http://www.asciiarmor.com/post/33736620/java-xpath-1-0-engine-comparison-performanceMy question is more related to how interpreting the rule and orchestrating the parsing accordingly. I should have been more precise, sorry.
Damien
I think that if you want to use file names with XPath, then you should look at xquery. There is one more XPath lib that you might want to investigate it is called VTD-XML (of which I am the coauthor)
vtd-xml-author
Thanks, I've seen that schematron is supposed to be able to work with xquery instead of xpath but, unfortunately, the java implementation (probatron) only supports xpath so far. I'm gonna assess all this and also have a look at VTD-XML to build my solution.
Damien
A: 

Somebody on the JavaCC mailing list pointed me to the right direction, mentioning Schematron. It led me to Probatron which seems to be the best java implementation available.

Schematron web site claims that the language supports "jump across links and between XML documents to check constraints" but it seems Probatron doesn't allow that. I may not to tweak it or find a trick for that (like building a temporary XML document containing all my source documents). Apart from that, it looks Probatron is the right library for me.

Damien