JAXB - When property already defined and using bindings it's not working with *complexContent*

287 views Asked by At

Using JAXB to generate Java classes. I'm using the following XSD scheme.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://schemas.microsoft.com/developer/msbuild/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msb="http://schemas.microsoft.com/developer/msbuild/2003" elementFormDefault="qualified">

    <!-- Working -->
    <xs:complexType name="MetaTypeSimpleContent">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="Name" />
                <xs:attribute type="xs:string" name="Scheme" />
                <xs:attribute type="xs:string" name="Value" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- Not Working -->
    <xs:complexType name="MetaTypeComplexContent">
        <xs:complexContent>
            <xs:extension base="msb:BaseType">
                <xs:attribute name="MyName" type="xs:string" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="BaseType">      
        <xs:attribute name="MyName" type="xs:string" />                
    </xs:complexType>

</xs:schema>

..and the following Binding definition

<?xml version="1.0" encoding="UTF-8"?>
<bindings   xmlns="http://java.sun.com/xml/ns/jaxb" 
            xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" 
            xmlns:xs="http://www.w3.org/2001/XMLSchema" 
            version="2.1">

    <bindings schemaLocation="Test.xsd" version="1.0">
        <!-- Customise the package name -->
        <schemaBindings>
            <package name="com.example.schema" />
        </schemaBindings>

        <!-- Working -->
        <bindings node="//xs:complexType[@name='MetaTypeSimpleContent']">
            <bindings node=".//xs:attribute[@name='Value']">
                <property name="ValueAttribute" />
            </bindings>
        </bindings>

        <!-- Not Working -->
        <bindings node="//xs:complexType[@name='MetaTypeComplexContent']/xs:complexContent/xs:extension/xs:attribute[@name='MyName']">            
            <property name="MyName3" />
        </bindings>

    </bindings>

</bindings>

The binding for the complextType MetaTypeSimpleContent with the simpleContent works fine. But the binding for the complexType MetaTypeComplexContent with the complexContent doesn't. Why? I got the following Error:

[ERROR] ct-props-correct.4: Error with type 'MetaTypeComplexContent'. Duplicate attribute usages were specified with the same name and target namespace. The name of the duplicate attribute is 'MyName'.
   Line 19 of file: / D: /temp/Stackoverflow/Test.xsd

A schema could not be parsed.
1

There are 1 answers

0
kimbert On

The error message seems to be pointing out the problem quite clearly:

[ERROR] ct-props-correct.4: Error with type 'MetaTypeComplexContent'. Duplicate attribute usages were specified with the same name and target namespace. The name of the duplicate attribute is 'MyName'.

When extending a complex type, you should not re-declare the inherited content. Maybe you intended to add a new attribute in the extended complex type, but you forgot to change the name?

To fix the error, do one of the following:

  • delete the complex type extension
  • change the name or namespace of the attribute in the complex type extension