Symbol is already defined. Use JAXB property to resolve the conflict

13,313

Let me ask about this line:

<xs:element ref="Symbol"/>

is Symbol defined in yahoo.xsd or locally in the same xsd file?

I'll try to deduce some facts.

I assume you have two XSDs: yahoo.xsd and some.xsd (first one in your post). I have strong confidence "Symbol" type is defined in some.xsd and not in yahoo.xsd. If it were otherwise i would expect some namespace prefix ("yahoo:Symbol" ?).

Now, is it true your some.xsd looks similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" >
    <!-- It's not important right now: -->
    <!--<xs:import namespace="http://www.yahooapis.com/v1/base.rng" schemaLocation="yahoo.xsd"/>-->

    <!-- declaration you omitted in your post, it's only example -->
    <xs:element name="Symbol">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
              <xs:minInclusive value="0"/>
              <xs:maxInclusive value="100"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:element name="quote">
        <xs:complexType>
          <xs:sequence>  
            <xs:element ref="Symbol"/>
          </xs:sequence>
          <xs:attribute name="symbol" use="required" type="xs:NCName"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

If what i say is true, then your jaxb binding should look like this:

<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="some.xsd"> <!-- not yahoo.xsd -->
        <bindings node="//xs:element[@name='quote']/xs:complexType/xs:sequence/xs:element[@ref='Symbol']">
            <property name="SymbolAttribute" />
        </bindings>
    </bindings>

</bindings>

And generated java class will be:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "symbolAttribute"
})
@XmlRootElement(name = "quote")
public class Quote {

    @XmlElement(name = "Symbol")
    protected int symbolAttribute;
    @XmlAttribute(name = "symbol", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String symbol;
    ....
Share:
13,313
Nicolas
Author by

Nicolas

Updated on June 04, 2022

Comments

  • Nicolas
    Nicolas about 2 years

    I have a xsd file (yahoo.xsd) where I import another xsd file like this:

      <xs:import schemaLocation="stock.xsd"/>
      <xs:attribute name="lang" type="xs:NCName"/>
    

    The stock.xsd looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng">
    <xs:import namespace="http://www.yahooapis.com/v1/base.rng" schemaLocation="yahoo.xsd"/>
    <xs:element name="quote">
    <xs:complexType>
      <xs:sequence>  
        <xs:element ref="Symbol"/>
      </xs:sequence>
      <xs:attribute name="symbol" use="required" type="xs:NCName"/>
    </xs:complexType>
    </xs:element>
    <xs:element name="Symbol" type="xs:NCName"/>
    </xs:schema>
    

    When I am compiling with xjc I am getting the following error message:

    [ERROR] Property "Symbol" is already defined. Use <jaxb:property> to resolve this conflict.

    I basically found the solution for this here on SO (JAXB Compiling Issue - [ERROR] Property "Any" is already defined) but I can't get it to work. I am guessing my XPath is wrong.

    This is the binding file I am using:

    <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="yahoo.xsd" version="1.0" >
        <!-- rename the value element -->
            <bindings node="//xs:element[@name='quote']/xs:complexType/xs:sequence/xs:element[@ref='Symbol']">
                <property name="SymbolAttribute"/>
        </bindings>
    </bindings>
    

    If I am now compiling with xjc -b it says that the XPath evaluation results in an empty target node.

    I probably have to rename the Symbol definition and then the ref as well? how to do this automatically?

  • Nicolas
    Nicolas over 11 years
    thanks: I already tried that out but it didnt work. But another thing you said is probably the root of the problem: Symbol is defined somewhere else and only referenced at the line I tried to change with my XPath. I added that to my original post. I guess I have to automatically rename both of them? Or does Jaxb automatically update all references to a renamed node? I cant try it out right now but will check it this evening.
  • Nicolas
    Nicolas over 11 years
    okay, so turns out this was exactly the problem. Changing the bindings node to <bindings node="//xs:element[@name='Symbol']"> solves the problem.