Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD - element nested in the element with the same name - is it proper?

Tags:

xml

jaxb

xsd

xjc

question as in the subject: Is it valid to define an element nested in element of the same name in XSD?

As an example, is the following snippet correct?

<xs:element name="TypeOfAction" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="TypeOfAction" minOccurs="0">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="code" type="xs:string"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

If so, how do you make xjc to properly generate classes for that? I got compilation errors like static class TypeOfAction is already defined (basically I would get two nested static classes of the same name generated).

like image 489
Michal Pasinski Avatar asked Oct 17 '25 10:10

Michal Pasinski


1 Answers

The snippet is fine. Only global elements need to have unique qualified name (combination of namespace and local name).

XJC from JDK 7 works fine with your snippet. My sample XSD generates correct classes.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="SomeType">
        <xs:sequence>
            <xs:element name="TypeOfAction" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="TypeOfAction"
                            minOccurs="0">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="xs:string">
                                        <xs:attribute name="code"
                                            type="xs:string" />
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema> 

But it doesn't compile because Java doesn't allow to declare inner class with the same name as outer class. Try changing generated type name using xjc bindings or inline schema annotations. You can find example here.

Sample inline binding:

...
<xs:element name="TypeOfAction"
    minOccurs="0">
    <xs:complexType>
        <xs:annotation>
            <xs:appinfo>
                <jaxb:class name="InnerTypeOfAction" />
            </xs:appinfo>
        </xs:annotation>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="code"
                    type="xs:string" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>
...
like image 130
Dawid Pytel Avatar answered Oct 19 '25 01:10

Dawid Pytel



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!