将s:element和s:complexType命名为相同的名称 [英] named the s:element and s:complexType the same name

查看:55
本文介绍了将s:element和s:complexType命名为相同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将s:element和s:complexType命名为相同名称是否合法?您可以看到以下代码,元素和复杂类型具有完全相同的名称这是wsdl文件的一部分.

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>

推荐答案

是的,在XML模式中具有相同名称的复杂类型和元素是合法的.

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

由于您的问题被标记为[java]和[web-services],因此您可能正在使用JAXB(通过JAX-WS)从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.

好猜!JAXB解析器抛出异常,主要是因为命名冲突,我的问题是,为什么JAXB不喜欢它是否合法它吗?

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)将复杂类型转换为Java类.对于命名复杂类型,结果类名是从复杂类型名派生的;对于匿名复杂类型,结果类名是从封闭元素派生的.在您的情况下,这将导致两个类具有JAXB抱怨的相同名称.

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.

重命名对应于XML元素的类(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>

重命名与复杂类型相对应的类(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>

这篇关于将s:element和s:complexType命名为相同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆