Search code examples
jaxbxjcmaven-jaxb2-plugin

Is there a way to ignore empty nodes in JAXB bindings file?


I am trying to create a generic .xjb bindings file to provide consistent Java classes generation from WSDLs / XSDs across multiple projects. We generate the code via maven-jaxb2-plugin (Made by @lexicore).

The problem lies in the multiple projects part. If a particular binding instruction matches nothing in the provided XSD or WSDL, the class generation fails with

XPath evaluation of "<some_xpath_expression>" results in empty target node

How can I tell JAXB to ignore these cases so the bindings file can be used on any project without fine-tuning, regardless of the elements types used?

Here is a (stripped down) version showcasing the problem I have:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"                
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              jxb:extensionBindingPrefixes="xjc">
    <jxb:bindings schemaLocation="path/to/the/schema" node="/xs:schema">
        <jxb:bindings multiple="true" node="//*[@type='xs:dateTime']">
            <xjc:javaType name="java.time.LocalDateTime" adapter="a.b.c.LocalDateTimeAdapter" />
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

If I try to generate classes from a schema with no dateTime element, it will fail.

The objective is, in the end, to create something all projects of various teams could reuse without changing anything but the schemaLocation.


Solution

  • You need to set the required="no" attribute on the binding that you would like to allow not to match any node, i.e.

    <jxb:bindings required="no" multiple="true" node="//*[@type='xs:dateTime']">
    

    Depending on your context you may choose to say required="false" or required="0" as well.