问题
My schema is:
<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>
And my xml will be:
Case 1.
<SetMonitor
xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:cb="http://schemas.cordys.com/1.0/coboc">
<period/>
<refreshrate/>
</SetMonitor>
OR Case 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>
For case 2 there are no issues. But for case 1 I get the following error:
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'.
How can i modify the wsdl so that it accepts both case 1 and case 2? Please help.
回答1:
You could do something like this:
<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>
And construct xml with "empty" element in this way
<SetMonitor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<period>2147483647</period>
<refreshrate xsi:nil="true" />
</SetMonitor>
Or you could modify the type of element using pattern, something like this
<xsd:element name="period">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="|([1-9][0-9]*)" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
(the pattern has to be define more precisly, than I use for this example)
Another possibility could be to define simpleType for empty string
<xsd:simpleType name="emptyString">
<xsd:restriction base="xsd:string">
<xsd:length value="0"/>
</xsd:restriction>
</xsd:simpleType>
and then define your element as union of xsd:long and emptyString type
<xsd:element name="period">
<xsd:simpleType>
<xsd:union memberTypes="xsd:long emptyString"/>
</xsd:simpleType>
</xsd:element>
来源:https://stackoverflow.com/questions/17313022/element-typelong-without-content