元素类型(长)没有内容

时间:2013-06-26 06:30:28

标签: xml wsdl xsd xsd-validation

我的架构是:

 <xsd:element name="SetMonitor">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="period" type="xsd:long" />
            <xsd:element name="refreshrate" type="xsd:long" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

我的xml将是:

案例1。

<SetMonitor
    xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:cb="http://schemas.cordys.com/1.0/coboc">
    <period/>
    <refreshrate/>
</SetMonitor>

案例2。

 <SetMonitor
        xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:cb="http://schemas.cordys.com/1.0/coboc">
        <period>10</period>
        <refreshrate>20</refreshrate>
    </SetMonitor>

对于案例2 ,没有问题。但对于案例1 ,我收到以下错误:

Caused by: org.xml.sax.SAXException: cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'.
org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 14; cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'.

如何修改wsdl以便它同时接受案例1 案例2 ? 请帮忙。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

    <xsd:element name="SetMonitor">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="period" type="xsd:long"  nillable="true"/>
                <xsd:element name="refreshrate" type="xsd:long" nillable="true"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

以这种方式用“empty”元素构造xml

<SetMonitor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <period>2147483647</period>
    <refreshrate xsi:nil="true" />
</SetMonitor>

或者您可以使用模式修改元素的类型,类似这样

<xsd:element name="period">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="|([1-9][0-9]*)" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

(模式必须更精确地定义,比我在本例中使用的那样)

另一种可能性是为空字符串定义simpleType

<xsd:simpleType name="emptyString">
    <xsd:restriction base="xsd:string">
        <xsd:length value="0"/>
    </xsd:restriction>
</xsd:simpleType>

然后将您的元素定义为xsd:long和emptyString类型

的并集
<xsd:element name="period">
    <xsd:simpleType>
        <xsd:union memberTypes="xsd:long emptyString"/>
    </xsd:simpleType>
</xsd:element>