如何在WSDL的Schema内为指定元素设置不同命名空间?
解决WSDL中指定元素命名空间的问题
这个问题核心是要让特定元素脱离父元素的命名空间,绑定到你想要的ww命名空间上,咱们直接修改WSDL的schema部分就行,不会影响其他元素的命名空间。
修改步骤和说明
原来的WSDL里,Message和ServiceID继承了父元素的http://tempuri.org/命名空间,所以生成的SOAP请求里它们带tem:前缀。要改成ww:,需要做两处调整:
- 在
tempuri.org的schema中引入ww命名空间,并给目标元素指定命名空间属性 - 确保
ww命名空间下有这两个元素的类型定义(如果之前没有的话)
修改后的WSDL代码片段
<ws:types> <!-- 原tempuri.org的schema,添加命名空间引用和元素指定 --> <xs:schema targetNamespace="http://tempuri.org/" xmlns:ww="http://ww" elementFormDefault="qualified"> <!-- 引入ww命名空间的schema定义 --> <xs:import namespace="http://ww"/> <xs:element name="GenericNode"> <xs:complexType> <xs:sequence> <xs:element name="request"> <xs:complexType> <xs:sequence> <!-- 给Message和ServiceID指定ww命名空间 --> <xs:element name="Message" minOccurs="0" maxOccurs="1" namespace="http://ww"/> <xs:element name="ServiceID" minOccurs="0" maxOccurs="1" namespace="http://ww"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> <!-- 补充ww命名空间的元素定义(如果之前没有的话) --> <xs:schema targetNamespace="http://ww" elementFormDefault="qualified"> <!-- 这里可以根据实际需求修改类型,比如xs:string、xs:anyType或者自定义复杂类型 --> <xs:element name="Message" type="xs:anyType"/> <xs:element name="ServiceID" type="xs:anyType"/> </xs:schema> </ws:types>
修改后生成的SOAP请求
调整后,SoapUI生成的请求会变成这样,Message和ServiceID会使用ww:前缀,其他元素保持tem:不变:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:ww="http://ww"> <soapenv:Header/> <soapenv:Body> <tem:GenericNode> <tem:request> <!--Optional:--> <ww:Message>anyType</ww:Message> <!--Optional:--> <ww:ServiceID>anyType</ww:ServiceID> </tem:request> </tem:GenericNode> </soapenv:Body> </soapenv:Envelope>
额外说明
如果你的系统中已经有单独的http://ww命名空间schema文件(比如已经定义了Message和ServiceID),那只需要保留xs:import语句,不用重复写第二个schema块,直接引用外部schema即可。这样既保证了命名空间的正确性,又不会冗余定义。
内容的提问来源于stack exchange,提问作者Marcos Rodrigues
相关产品推荐
相关产品推荐

