2014-09-11 61 views
1

我定义为SOAP请求XSD模式被发送给接受这样的请求的Web服务:我应该如何定义我的XSD以将动态类型用作元素?

<generic.GenericObject.configureInstanceWithResult xmlns="xmlapi_1.0"> 
    [..] 
    <configInfo> 
     <lte.Cell> 
      <actionMask> 
       <bit>modify</bit> 
      </actionMask> 
      <spare1>1</spare1> 
     </lte.Cell> 
    </configInfo> 
</generic.GenericObject.configureInstanceWithResult> 
<generic.GenericObject.configureInstanceWithResult xmlns="xmlapi_1.0"> 
    [...] 
    <configInfo> 
     <lte.LteNeighboringCellRelation> 
      <actionMask> 
       <bit>modify</bit> 
      </actionMask> 
      <cellIndividualOffset>-1</cellIndividualOffset> 
     </lte.LteNeighboringCellRelation> 
    </configInfo> 
</generic.GenericObject.configureInstanceWithResult>  

为了实现这个结果,我试图用这个模式定义:

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="xmlapi_1.0" xmlns:tns="xmlapi_1.0" elementFormDefault="qualified"> 

<complexType name="Generic.GenericObject.configureInstanceWithResult"> 
    <sequence> 
     [...] 
     <element name="configInfo" type="tns:ConfigInfo" /> 
    </sequence> 
</complexType> 

<complexType name="ConfigInfo"> 
    <sequence> 
     <element name="payload" type="anyType" /> 
    </sequence> 
</complexType> 

<complexType name="lte.Cell"> 
    <sequence> 
     <element name="spare" type="string" /> 
    </sequence> 
</complexType> 

<complexType name="lte.LteNeighboringCellRelation"> 
    <sequence> 
     <element name="qOffsetCell" type="string" /> 
     <element name="cellIndividualOffset" type="string" /> 
    </sequence> 
</complexType> 

<element name="generic.GenericObject.configureInstanceWithResult" type="tns:Generic.GenericObject.configureInstanceWithResult" /> 

但结果我得到的是这样一个:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<generic.GenericObject.configureInstanceWithResult xmlns="xmlapi_1.0"> 
    <configInfo> 
     <payload xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="lte.Cell"> 
      <spare>-1</spare> 
     </payload> 
    </configInfo> 
</generic.GenericObject.configureInstanceWithResult> 

您是否看到过我可以用xsi:type="lte.Cell"作为<lte.Cell>而不是<payload>

注意:在ConfigInfo中使用anyType不好,因为<configInfo>被删除,请求不再符合要求。

回答

0

至于评论,我认为,在这里,他们最好的办法是让any为元素<ConfigInfo>

<complexType name="ConfigInfo"> 
    <sequence> 
     <any minOccurs="0" maxOccurs="1"/> 
    </sequence> 
</complexType> 

这可以让你有任何类型的标记@XmlRootElement在XML介绍对象的。

在我的模式有资格获得“有效载荷”定义两类这种特殊情况下,元素声明需要引入,使他们正确地标记为@XmlRootElement

<element name="lte.Cell" type="tns:Lte.Cell" /> 
<element name="lte.LteNeighboringCellRelation" type="tns:Lte.LteNeighboringCellRelation" /> 
0

将有效载荷转换为全局元素声明(最好使用abstract =“true”),并为lte.Cell和lte.LteNeighboringCellRelation创建指定substitutionGroup =“payload”的元素声明。

+0

谢谢迈克尔。我正在考虑这个选项,但问题是你必须指定被用作“有效载荷”的对象的类型。这里的问题是,这个对象是配置大量不同类型的“API”的基础,所以如果我们做一些真正动态的事情(没有任何硬编码)会更好。我目前正在尝试使用“xs:any”的解决方案,并使用@XMLRootElement标记每个元素,我认为它们能正常工作。 – Victor 2014-09-11 14:19:55

相关问题