I have been using xjc to compile XML Schema into annotated java classes, so that I can generate xml files using jaxb.
How can I do the same for Schematron?
UPDATE: To be more specific: The java classes don't need to validate all the schematron rules, only the parts that pertain to schema, i.e. the parts of the Schematron that could have been expressed as XSD.
This is the specific schema I want to compile: https://github.com/OpenPEPPOL/peppol-bis-invoice-3/blob/master/rules/sch/PEPPOL-EN16931-UBL.sch
The purpose of this is to generate PEPPOL BIS Billing 3.0 invoices and credit notes. Other comments also welcome.
Schematron is not a schema definition as is the case with XSDs when using XJC. It is not possible to generate Java classes from Schematron using XJC.
XSD is a grammar that is able to validate a XML structure.
Schematron are rules that helps (not exclusive) to validate business rules on data, not on structure.
If I made a parallel with JAXB generated classes, XSD is able to validate that each XML node fits into a class attribute ; Schematron validates that invoice.vat < invoice.totalAmount. So Schematron rules are usually added to a structural grammar (XSD, RelaxNG, DTD). You'd be able to define some validate() method on your JAXB classes that matches your schematron rules.
I remember a project (15 years ago) that included some Schematron rules into an Xsd grammar, and help to generate validation methods from this. But generated code wasn't very precise, and most of XPath expressions couldn't be transformed into java.
That was based on https://github.com/NIEM/Schematron-in-XSD-Spec but I can not found it.
If I had to implement this, I'll have a look at https://phax.github.io/ph-schematron/ and I'll generate a proxy around JAXB classes that will validate against Schematron rules.
Best regards,
Christophe
Related
I am currently working on an application that performs the task of importing or exporting some entities. The file format being used for the same is XML. JAXB is being used for XML binding.
The problem is present XSD that defines the structure of entities has no provision for versioning. How do I get started with defining versioned XSD and subsequently XML instance documents provided JAXB lies as the underlying binding framework ?
I have read that there are three possible ways of introducing versions in XSD.
1) Change the internal schema version attribute
2) Create a attribute like schemaVersion on the root element
3) Change the schema's target namespace.
Which one best suits the usecase mentioned below?
Use case: The changes made to the XSD in the next version may invalidate the existing elements. Although the schema itself may not be backward compatible but the application needs to provides support for handling all versions of schema.
XML is designed to facilitate change and flexibility in document structures. Unfortunately, JAXB isn't. The very act of compiling knowledge of document structure into your Java source code makes change to the document structure a lot more difficult.
If structural change is part of your agenda, I think you should seriously consider not using JAXB: technologies like XQuery and XSLT are much better suited to this scenario.
Is there an XML serialization framework for Java that does not use reflection, but instead generates static serialization code (Java source) from XSD ?
I've never seen anything that does exactly what you are asking for: generating serialization code from XSD. However, if you're not stuck with an existing XSD schema, Modello may satisfy your requirements.
Modello is used by Maven for parsing pom.xml and settings.xml files. It reads a .mdo file (like this description of the Maven project model), and can generate a Java object model; an XML Schema (XSD) file; and serialisation/de-serialisation code. The serialisation/deserialisation code can use one of a number of XML parser APIs (e.g. JDOM, StAX, etc.). The XML parser API used by Maven itself is xpp3.
Modello can also generate code to convert one version of the model to another. It can generate HTML documentation about your XML format.
If you have an existing XSD, it might be too much work to use modello. But, if you're creating your own XML format, it could be worth starting with modello and generating the XSD.
I have a xsd that in turn uses/imports a set of xsds.
I would like to programtically generate sample xml from the xsd. The xml must contain all elements and attributes populated with example data based on data type.
How can I do this using eclipse api classes?
Also are there any other tools that accomplish this task and can be evoked in a java program or a batch file?
Any pointers to examples/documentation/api is highly appreciated.
Thanks in advance.
if I am reading your question correctly, I believe what you are trying to do is programmatically generate (i.e. using Java) XML documents based on an XML Schema Document (which may in turn import other supporting XSD's).
You may wish to have a look at Oracle/Sun's JAXB (Java Architecture for Xml Binding) which you can find more info about here:
http://jaxb.java.net/
JAXB works with J2SE-SDK and/or IDEs - such as Netbeans or Eclipse, and permits you to unmarshall (read XML documents into mapped Java Objects) or marshall (write Java objects as XML documents) as required. Standard mappings (known as binding declarations) are provided based on valid XML Schema provided to JAXB. You can also provide binding declarations through custom annotations directly within your XML Schema files or by using external JAXB declarations.
Another alternative (similar to JAXB) is Apache's XML-Beans.
Hope this helps!
The .NET Framework exposes a rather useful set of classes as part of the System.Xml.Schema namespace.
Among other things, classes such as XmlSchema and XmlSchemaElement provide a useful API for defining/writing and parsing/reading/traversing XML schema files.
Does an equivalent library exist for Java?
The tools to accomplish this are there, but I don't think there's anything specific like that in Java. What I would do is run JAXB on the XML Schema schema (http://www.w3.org/2001/XMLSchema), which will generate Java classes corresponding to the XSD constructs. Then you can use those to read/write XML schemas, and validate against the XML Schema schema.
Given a few hundred java source files how can I generate an xsd schema that describes their class hierarchy, attributes and methods?
Most of the resources I've seen describe how to convert java to xml given the schema but how is that schema created? I did find Simple but that requires me to alter the source which I am not allowed to do.
Apache axis provides the java2wsdl tool. Sure, you didn't ask for a wsdl but this tool should generate schemas (as needed) too.
So it's worth trying: implement some dummy interfaces with methods, that just use the types from your 'hundreds' of files, generate a wsdl and delete everything but the schemas.
XSD schemas in themselves are recipies allowing a program to say whether a given XML document "conforms"/"does not conform" to the schema.
For this to make sense for Java classes, you have to define a conversion scheme from Java source to XML, and that XML format can then have an XSD, which may or may not be easily derivable programatically.
What Java -> XML mapping do you use?