Friday, May 8, 2009

JAXB xjb & too many nodes

As I went into a while ago, you can use xjb to customize JAXB bindings. Today I learned of a tragic shortcoming. So say you have this snippet
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
 version="1.0">
 <jaxb:bindings schemaLocation="train.xsd" node="//xsd:element[@name='car']">
  <jaxb:property name="cars" />
 </jaxb:bindings>
</jaxb:bindings>
In that example there was a complexType with the name train, which contained an element with the name car which corresponded to the complexType car. All is right in the world. But what if there was another element somewhere, probably in some other complexType that contained a car? Well, that'd be kosher unless you were trying to customize the bindings with the above snippet. Instead you'll end up seeing
[ERROR] Error while parsing schema(s).Location [ file:/C:/Dev/Workspace_Netbeans /my-project/src/main/resources/train.xjb{5,80}].com.sun.istack.SAXParseException2: XPath evaluation of "//xsd:element[@name='widget']" results in too many (2) target nodes
There was an enhancement request put in years ago for XPath to be able to match multiple nodes, but like Bill O'Reilly releasing a softcore romance novel I wouldn't hold my breathe. Oh wait.

3 comments:

  1. Add attribute multiple="true" to jaxb:binding element which results to multiple nodes. It should work now (tested right now)

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete