I want to validate from Java code a schematron condition (which is in the end a xslt boolean evaluation) to know if it is syntactically correct. Our users can provide extra validation rules that we will transform into schematron to validate given XML files, but we want to know if these rules make sense in the end.
I have tried with method compile from javax.xml.xpath.XPath, but seems to lack things like 'castable as' and other default xslt2.0 functions/operators. I've tried to provide a default XPathFuntionResolver to tell the XPath to ignore the functions, but didn't seem to do the trick.
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setXPathFunctionResolver(new XPathFunctionResolver() {
#Override
public XPathFunction resolveFunction(QName functionName, int arity) {
return new XPathFunction() {
#Override
public Object evaluate(List args) throws XPathFunctionException {
return null;
}
};
}
});
Which is the best way to validate those user defined rules?
The default implementation of javax.xml.xpath.XPath in the JDK only supports XPath 1.0, but your particular XPath expressions are XPath 2.0 (or later). There are several third-party implementations of XPath 2.0 available for the Java platform, the Saxon product (mine) is the best known.
You can use Saxon's XPath engine using the standard javax.xml.xpath.XPath API, as Martin Honnen suggests; however the API is designed around XPath 1.0 so it can be tricky to exploit the full functionality of XPath 2.0 (for example, you can't easily evaluate an expression that returns a date). So Saxon has an alternative API called s9api, which is probably easier to use. For details the Saxon documentation is at www.saxonica.com.
Your approach of using a custom function resolver isn't going to help the XPath 1.0 engine understand syntactic constructs like "X castable as Y" - it's only used to resolve function calls.
Related
I am upgrading from XPath 1.0 to XPath 3.1. I used the library javax.xml.xpath for XPath 1.0 and am now using the Saxon-HE library. It was fairly straightforward to migrate the code and I used the S9API interface for the evaluation of the expressions as suggested by saxon.
There is however one little part of code that I can not seem to figure out how to migrate:
public static Object getXpathValueFromContentMap(String name, Map<String, Object> content, boolean list) {
try {
if (list) {
return newArrayList(JXPathContext.newContext(content).iterate(name));
} else {
return JXPathContext.newContext(content).getValue(name);
}
} catch (JXPathNotFoundException ignore) {
return null;
}
}
This method evalutes an xpath expression on a map. Can this be done using the saxon library and if so, how?
JXPath (which I haven't come across before) appears to be an implementation of XPath 1.0 over (an XML node view of) Java data structures such as maps and arrays.
Saxon doesn't have anything similar. With XPath 3.1 it would be much more appropriate to interpret Java maps and arrays as XPath 3.1 maps and arrays, rather than going via an XML node view. However, the XPath expressions for doing that will be significantly different.
It's theoretically possible to provide an XML mapping in Saxon over an external data structure but it's a significant amount of work and that doesn't seem the right approach. But it rather depends on how much XPath code you've got that needs converting.
I am trying to parse a mathematical formula to a subset of LaTeX using ANTLR4. For example it should parse (a+4)/(b*10) to \frac{a+4}{b\cdot 10}.
My simple grammar creates a tree like this:
Now I am trying to implement parse tree listeners to somehow construct the LaTeX String while the tree is traversed. Here, I am failing because to construct a String like \frac{}{} it has to be built recursively. The parse tree walker, however, visits one tree node after the other (in a breadth-first way as far as I can tell).
I've read about parse tree visitors that might be what I need. But I wasn't able to find some examples how these visitors are applied.
Could you provide an example how parse tree listeners/visitors can be used in this particular case? Do you think my approach to use ANTLR for the parser makes sense in the first place?
You can create a parse tree walker by implementing the ParseTreeVisitor interface. For ease of use, you can specify Antlr to generate a base visitor when compiling the grammar (in Antlrworks, Run->Generate Recognizer->Next->Generate Visitor->Next->Finish). The base visitor will be called MyGrammarBaseVisitor. Note that the visitor has a generic type T, which every single visit method should return. I recommend using Void for manual manipulation or String for ease of use during code generation.
After you extend the base visitor (I'll assume here we're dealing with String), you need to override the visit methods. These methods are named after the grammar rules you have. Each of these methods will receive a ParserContext ctx parameter, which you use to visit child rules and/or get terminal values. For example, you could do this:
class MyVisitor extends MyGrammarBaseVisitor<String> {
#Override
public String visitMultiplicative(MyGrammarParser.MultiplicativeContext ctx) {
if (ctx.opMult().getText().equals("/")) return "\\frac{" + visit(ctx.expr(0)) + "}{" + visit(ctx.expr(1)) + "}";
else return visit(ctx.expr(0)) + "\\cdot " + visit(ctx.expr(1));
}
// visit methods for other rules...
}
I'm assuming your multiplicative rule looks like multiplicative: expr opMult expr; opMult: '*' | '/'; You can find more information in The Definitive Antlr 4 Reference. You may also find more information and examples in the Antlr documentation.
I am setting the parameter like this:
Document doc_23 = createDocument(doc_bytes);
XPathExpression xpe = XPathFactory.newInstance().newXPath().compile("/");
transformer.setParameter("document23",xpe.evaluate(doc_23, XPathConstants.NODESET));
I also tried this:
transformer.setParameter("document23",new StreamSource(new StringReader(xml_text)));
In my xslt i am getting the variable like this:
<xsl:variable name="document23" select="/.."></xsl:variable>
And try to use it:
<xsl:for-each select="$document23//Product">
<xsl:message>Test<xsl:value-of select="generalDetails/productCode"/></xsl:message>
</xsl:for-each>
But i dont work (the for-each never get enterd).
The document have the elements speciffied beacasue using 'document(document23.xml)//Product' does work.
Thanks for the help.
You're using the DOM and JAXP APIs, which isn't an ideal way of using Saxon: the DOM is very slow with Saxon, and the JAXP XPath API is very weakly-typed so you need to have the interface specification and the Saxon-specific details both to hand in order to use it successfully. So my first recommendation would be, if you're committed to Saxon then you would be better off using the s9api API in preference.
In fact I don't understand why you are using XPath interfaces at all. You seem to be trying to run the XPath expression "/", which returns whatever you supply as the input. That's completely pointless.
If you do want to use the JAXP transformation API (and therefore setParameter()), the kind of things you can supply are described here:
http://www.saxonica.com/documentation/index.html#!using-xsl/embedding/jaxp-transformation
In particular see the paragraph that starts "The types of object that can be supplied as stylesheet parameters ..." This links to the "Extensibility" section, which tells you
"If the [...] value is an instance of javax.xml.transform.Source (other than a NodeInfo), a tree is built from the specified Source object, and the root node of this tree is returned as the result of the function."
So you can supply a StreamSource or a DOMSource to the setParameter() method as in your example.
If the path expression in an xsl:for-each appears to be selecting nothing, use xsl:message or xsl:copy-of to display the document you are trying to select into; this will often give you a clue what is wrong.
I see one problem, mainly that you have used <xsl:variable name="document23" select="/.."></xsl:variable>, if you want to define an external parameter then you need to use <xsl:param name="document23" select="/.."/>, not xsl:variable.
JAXB has been great, a real timesaver, but it's still really time consuming to traverse the resulting object trees; almost as bad as working directly with the DOM.
Is there a way that I can do XPath 1.0 queries on a JAXBElement, without having to painstakingly marshal the document to and from a DOM model each time?
Not directly, no. However, you can use Apache Commons Jxpath, which allows you to run XPath queries across arbitrary object graphs, not just JAXB-bound ones. It can be run in "lenient" mode, which is tolerant of nulls.
Extremely handy for replacing those NPE-prone graph navigations.
The accepted answer was from 2010 and this post is for the benefit of others who are looking to use XPath with JAXB. Moxy implementation provides lots of nice extensions and one of them is to execute XPath. Read more about this on Moxy's tutorial. Example copied from the same place
Customer customer = (Customer) jaxbContext.createUnmarshaller().unmarshal(instanceDoc);
...
int customerId = jaxbContext.getValueByXPath(customer, "#id", null, Integer.class);
jaxbContext.setValueByXPath(customer, "first-name/text()", null, "Bob");
jaxbContext.setValueByXPath(customer, "phone-number/area-code/text()", null, "555");
...
jaxbContext.createMarshaller().marshal(customer, System.out);
Is there is Simple way to read and write Xml in Java?
I've used a SAX parser before but I remember it being unintuitive, I've looked at a couple of tutorials for JAXB and it just looks complicated.
I don't know if I've been spoilt by C#'s XmlDocument class, but All I want to do is create an Xml Document that represents a a set of classes and their members (some are attributes some are elements).
I would look into serialization but the XML has to have the same format as the output of a c# app which I am reverse engineering into Java.
I recommend XOM. Its API is clear and intuitive.
You should check out Xstream. There is a 2 minute tutorial that is really simple. To get the same format, you would model the classes the same.
If you are using jdk 1.4 or newer take a look at XMLEncoder class.
Some of the more popular approaches to consider:
Java Archictecture for XML Binding
JAXB is a specification for a standard XML binding. If you already have an XSD, it can generate your Java classes for you, and then all that's left is to use a standard API for marshalling/unmarshalling.
Reference implementation from Glassfish
Apache's implementation JaxMe
Other binding approaches
As with JAXB, these approaches use XML-based binding configurations. They may provide more fine grained control of the unmarshalling process.
Castor
JIBX
Roll your own
Using StAX
Using XOM
Using plain XPath
Dom4j is a simple api for creating xml documents in java.
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "root" );
Element author2 = root.addElement( "author" )
.addAttribute( "name", "Toby" )
.addAttribute( "location", "Germany" )
.addText( "Tobias Rademacher" );
The most simple way so far is the MarkupBuilder in Groovy. Think of Groovy as a new syntax for Java. The XmlSlurper can be used to read XML.
I think that Apache XMLBeans provides the functionality you are after.
The Wikipedia page gives a good overview and example usage.
There is a wide choice of XML processing options for Java, though judging from the .NET documentation for XmlDocument, the Java DOM implementation is the closest out-of-the-box equivalent.
.NET XmlDocument:
This class implements the W3C Document
Object Model (DOM) Level 1 Core and
the Core DOM Level 2.
Java Document:
See also the Document Object Model (DOM) Level 3 Core Specification.
Sample code:
public static void main(String[] args) throws Exception {
File xmlFile = new File(".classpath");
// read it
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlFile);
// walk it
System.out.println("Node count=" + countNodes(document));
// write it
Source source = new DOMSource(document);
Result result = new StreamResult(System.out);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
}
/** Doesn't count attributes, etc */
private static int countNodes(Node node) {
int count = 0;
NodeList kids = node.getChildNodes();
count += kids.getLength();
for (int i = 0; i < kids.getLength(); i++) {
count += countNodes(kids.item(i));
}
return count;
}
I think JAXB is only complicated if you look at wrong examples. Specifically, yes, schema-based way can get messy. But code-first, annotation-based is trivially easy.
Another easy alternative is XStream. And for non-binding case, StaxMate, which is an add-on for streaming Stax parsers.
If SAX parsing is mandatory, JAXP is a good choice. I prefer DOM parsing and use jdom which seems a lot easier to me.
I would certainly use XOM if you want a DOM-like approach and SAX (www.sax.org) if you want a SAX-like approach. I was involved in the early development of XML and SAX was developed as an event-driven approach, which is useful for some applications. DOM/XOM and SAX are complementary - sometimes you need one, sometimes the other. If you wish to build objects as you go rather than read everything into memory, use SAX. If you are happy to read everything in and then process it, use XOM.
I spent far too much time trying to get the W3C DOM to work - IMO it is poorly defined with too many ways of doing some things and not enough for others. When XOM came it revolutionised my productivity.
The XOM community is very knowledgeable and focused and helpful.