Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB binding: dynamic class names for repeated elements

Tags:

xml

jaxb

xpath

xsd

xjc

I have an XSD with repeated elements of name row that are generating collisions when trying to parse it with XJC. I would like to know if there's a way to append an index to each name so as to generate unique class names, such as Row1.java, Row2.java, Row3.java and so on.

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 tried using XPath expressions but I get rubbish output like _002f_002fXsElement_005b1_005d.java. Maybe the approach I'm taking is wrong. Any suggestions are welcome.

like image 733
CountD Avatar asked Dec 14 '25 12:12

CountD


1 Answers

This is not possible with the schema you have. You say row can be repeated an infinite number of time (given physical limits). How many classes should xjc generate? If the classes you describe are really what you want, then the schema you want is:

<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>

Actually, it is something much more complex, to ensure that row{n+1} appears only when row{n} is present, but you get the idea.

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

like image 116
Florent Georges Avatar answered Dec 17 '25 22:12

Florent Georges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!