Change the name of generated variable for referenced element in XSD using Jaxb and bindings

Eon picture Eon · Dec 4, 2012 · Viewed 11k times · Source

Today I have a mindboggler I just cannot solve. I will start with an Explanation and Example.

I have 2 XSD Files. The one XSD File References one of the other's elements.

First XSD- ReportInfo.xsd:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xs:schema id="ReportInfoWrapper" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="ReportInfoWrapper" >
    <xs:complexType>
      <xs:sequence>
          ...
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Second XSD- ReportInfoRecordSet.xsd:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xs:schema id="ReportInfoRecordSetWrapper" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:include schemaLocation="./ReportInfo.xsd" />
  <xs:element name="ReportInfoRecordSetWrapper">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="ReportInfoWrapper" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

ReportInfoRecordSet references ReportInfoWrapper (The root element of ReportInfo). I need to know what I would define in the JAXB Bindings file to change the generated name for this referenced element in ReportInfoRecordSet. This is what it currently generates:

public class ReportInfoRecordSetWrapper {

@XmlElement(name = "ReportInfoWrapper", required = true)
protected List<ReportInfoWrapper> reportInfoWrappers; //I need to change the name here in the bindings file.

The Question Any help or advice will be greatly appreciative. Note, I cannot make ReportInfo's Root element a complex Type, as it would break the current bindings file for ReportInfo. Is there any way to define the variable's name in the following notation? Note this example below does not work for some reason (I believe its node targeting issues":

      <jaxb:bindings node=".//xsd:element[@name='ReportInfoRecordSetWrapper']/xsd:complexType/xsd:sequence/xsd:node[@ref=ReportInfoWrapper"]">
         <jaxb:property name="records" />
      </jaxb:bindings>

Note

An easy way to see what I am trying here, I could explain in normal developing Terms.

ReportInfo is the "class"

ReportInfoRecordSet is an Array of ReportInfo classes.

EDIT

<jaxb:bindings
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    xmlns:annox="http://annox.dev.java.net"
    jaxb:extensionBindingPrefixes="xjc inheritance annox"
    version="2.1">
    <jaxb:globalBindings localScoping="toplevel">
        <xjc:simple />
        <xjc:javaType adapter="aem.adservices.google.dfa.utils.DateAdapter" name="java.util.Calendar" xmlType="xs:dateTime" />
    </jaxb:globalBindings>
    <jaxb:bindings schemaLocation="../xsd/ReportInfoRecordSet.xsd" >
      <jaxb:bindings node=".//xsd:element[@name='ReportInfoRecordSetWrapper']/xsd:complexType">
         <annox:annotate>
           <annox:annotate annox:class="aem.utilities.boomi.BoomiObject" label="ReportInfoRecordSet" description="ReportInfoRecordSet" OperationTypes="UPSERT"  />
         </annox:annotate>
      </jaxb:bindings>
       <jaxb:bindings node="//*/xs:element[@ref='ReportInfoWrapper']">
           <jaxb:property name="records"/>
       </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

The Error is thrown at line 19 saying XPATH error: null. Lines 19 - 21 are new lines added to my code.

EDIT 2

Remember kids, working with XJC requires you to double check which namespaces you provide to the XPATH Processor. I found the mistake where node="//*/xs:element[@ref='ReportInfoWrapper']" should be node="//*/xsd:element[@ref='ReportInfoWrapper']"

Answer

korifey picture korifey · Dec 4, 2012

This binding must work (checked locally):


<jxb:bindings version="1.0" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
  jxb:extensionBindingPrefixes="xjc">
    <jxb:bindings schemaLocation="ReportInfoRecordSet.xsd" node="/xs:schema">
       <jxb:bindings node="//*/xs:element[@ref='ReportInfoWrapper']">
            <jxb:property name="records"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>