JAXB Binding: Dynamic Class Names for Duplicate Elements

I have an XSD with duplicate name elements row

that generate collisions when trying to parse it with XJC. I would like to know, is there a way to add an index to each name to generate a unique class names, such as Row1.java

, Row2.java

, Row3.java

, etc.

sample.xsd

    <xsd:complexType name="table">
        <xsd:sequence>
            <xsd:element name="row" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>                   
                    <xsd:attribute name="id" type="xsd:integer"/>
                </xsd:complexType>
            </xsd:element>          
        </xsd:sequence>
    </xsd:complexType>

      

binding.xml

    <jxb:bindings version="2.1"
        xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">

        <jxb:bindings schemaLocation="sample.xsd">
            <jxb:globalBindings localScoping="toplevel" />
            <jxb:bindings node=".//xs:element[@name='row']" multiple="true" >
                <jxb:class name="RowXX"/>
            </jxb:bindings>
        </jxb:bindings>
    </jxb:bindings>

      

xjc command

    xjc -extension binding.xml sample.xsd 

      

I've tried using XPath expressions, but I'm getting garbage output for example _002f_002fXsElement_005b1_005d.java

. Perhaps the approach I am taking is wrong. Any suggestions are greatly appreciated.

+3


source to share


1 answer


This is not possible with a schema. You say that row

you can repeat an infinite amount of time (subject to physical limitations). How many classes should xjc generate? If the classes you describe are really what you want, then you need a schema:

<xsd:complexType name="table">
   <xsd:sequence>
      <xsd:element name="row1" minOccurs="0">
         <xsd:complexType>
            <xsd:attribute name="id" type="xsd:integer"/>
         </xsd:complexType>
      </xsd:element>
      <xsd:element name="row2" minOccurs="0">
         <xsd:complexType>
            <xsd:attribute name="id" type="xsd:integer"/>
         </xsd:complexType>
      </xsd:element>
      <xsd:element name="row3" minOccurs="0">
         <xsd:complexType>
            <xsd:attribute name="id" type="xsd:integer"/>
         </xsd:complexType>
      </xsd:element>
      ...
   </xsd:sequence>
</xsd:complexType>

      



In fact, it's something a little more complex to ensure that row{n+1}

it only appears when it is present row{n}

, but you get the idea.

My feeling: you have another problem that you are trying to solve and you decided that having numbered classes like this is the solution to that problem (s). If you determine why you want such classes in the first place, we might find a better solution.

+1


source







All Articles