我应该如何定义XSD以将动态类型用作元素



我正在为要发送到接受以下请求的Web服务的SOAP请求定义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>被删除,请求不再符合要求。

如前所述,我认为他们最好的方法是在<ConfigInfo>:中使用any作为元素

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

这允许您在XML中引入任何类型的用@XmlRootElement标记的对象。

在这种特殊的情况下,我在模式中定义了两个符合"有效负载"条件的类,需要引入元素声明,以便将它们正确标记为@XmlRootElement:

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

将有效载荷生成一个全局元素声明(最好使用abstract="true"),并为lte创建元素声明。Cell和lte。指定substitutionGroup="payload"的LteEighboringCellRelation。

最新更新