-->

元素类型(长)没有内容(Element type(long) without content)

2019-10-18 07:09发布

我的模式是:

 <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? 请帮忙。

Answer 1:

你可以这样做:

    <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>

并构建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>

(图案必须被定义更多precisly,比我使用本示例)

另一种可能定义为简单类型空字符串

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

然后定义元素的XSD的工会:长emptyString类型

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


文章来源: Element type(long) without content