Вопрос

is it legal to name the s:element and s:complexType the same name? you can see the following code, the element and complextype has exactly the same name this is a chunk of wsdl file.

<s:element name="ProcessTaskActionResponse">
 <s:complexType>
  <s:sequence>
    <s:element minOccurs="0" maxOccurs="1" name="ProcessTaskActionResult" type="tns:ProcessTaskActionResponse"/>
  </s:sequence>
 </s:complexType>
</s:element>

<s:complexType name="ProcessTaskActionResponse">
 <s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="TaskId" type="tns:ArrayOfServerId"/>
    <s:element minOccurs="1" maxOccurs="1" name="Success" type="s:boolean"/>
    <s:element minOccurs="0" maxOccurs="1" name="ActionPerformedLabel" type="s:string"/>
    <s:element minOccurs="0" maxOccurs="1" name="Errors" type="tns:ArrayOfString"/>
    <s:element minOccurs="1" maxOccurs="1" name="RemovedFromTaskList" type="s:boolean"/>
 </s:sequence>
</s:complexType>
Это было полезно?

Решение

Yes, it is legal to have a complex type and element with the same name in an XML Schema.

Since your question is tagged with [java] and [web-services] you may be using JAXB (via JAX-WS) to generate classes from the XML Schema. In that case since a complex type and element (with an anonymous complex type) have the same name you will need to use an external binding file to rename one of the generated classes.


UPDATE

good guess! the JAXB parser thrown a exception, mainly because the naming conflict, my question is if its legal why the JAXB not liking it?

JAXB (JSR-222) converts complex types to Java classes. For named complex types the resulting class name is derived from the complex type name, for anonymous complex types the resulting class name is derived from the enclosing element. In your case this would result in two classes with the same name that JAXB complains about.

Rename Class Corresponding to the XML Element (binding.xml)

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

    <jxb:bindings schemaLocation="your-schema.xsd">
            <jxb:bindings node="//xs:element[@name='ProcessTaskActionResponse']/complexType">
                <jxb:class name="ProcessTaskActionResponseElement"/>
            </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

Rename Class Corresponding to the Complex Type (binding.xml)

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

    <jxb:bindings schemaLocation="your-schema.xsd">
            <jxb:bindings node="//xs:complexType[@name='ProcessTaskActionResponse']">
                <jxb:class name="ProcessTaskActionResponseType"/>
            </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

Другие советы

Just regarding if it is correct to have an element and a ComplexType with the same name, here is the section of W3 that specifically authorize it:

http://www.w3.org/TR/xmlschema-1/

Section 2.5

“For example, the same name can appear in both a type definition and an element declaration, without conflict or necessary relation between the two.”

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top