xsd 浮点数和空值日期

Posted

技术标签:

【中文标题】xsd 浮点数和空值日期【英文标题】:xsd float and date with empty values 【发布时间】:2015-06-03 13:19:46 【问题描述】:

嗨下面是我的xml

<?xml version="1.0" encoding="UTF-8"?>
<CatalogObjects loadItemName="CatalogEntryOfferPrice">
<Catalog>
<PriceListUniqueId></PriceListUniqueId>
<PriceListName>MadisonsNominal Products Price List</PriceListName>
<CatentryUniqueId></CatentryUniqueId>
<CatentryPartNumber>012-300-050-RX</CatentryPartNumber>
<Identifier></Identifier>
<Precedence>0</Precedence>
<StartDate></StartDate>
<EndDate></EndDate>
<QuantityUnitIdentifier></QuantityUnitIdentifier>
<MinimumQuantity></MinimumQuantity>
<MaximumQuantity></MaximumQuantity>
<LastUpdate></LastUpdate>
<Description></Description>
<PriceInUSD>45</PriceInUSD>
<PriceInBRL></PriceInBRL>
<PriceInCAD></PriceInCAD>
<PriceInCNY></PriceInCNY>
<PriceInEUR></PriceInEUR>
<PriceInJPY></PriceInJPY>
<PriceInKRW></PriceInKRW>
<PriceInTWD></PriceInTWD>
<Delete>0</Delete>
</Catalog>

为此我创建了 xsd 如下

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  elementFormDefault="qualified">
 <xs:element name="CatalogObjects">
  <xs:complexType>
<xs:sequence>
<xs:element ref="Catalog" />
</xs:sequence>
<xs:attribute type="xs:string" name="loadItemName" fixed="CatalogEntryOfferPrice" use="required"/>
  </xs:complexType>
</xs:element>
<xs:element name="Catalog" >
 <xs:complexType>
<xs:sequence>
       <xs:element type="xs:string" name="PriceListUniqueId" />
       <xs:element type="xs:string" name="PriceListName" />
       <xs:element type="xs:string" name="CatentryUniqueId" />
       <xs:element type="xs:string" name="CatentryPartNumber" />
       <xs:element type="xs:string" name="Identifier" />
       <xs:element type="xs:byte" name="Precedence" />
       <xs:element type="xs:string" name="StartDate" />
       <xs:element type="xs:string" name="EndDate" />
       <xs:element type="xs:string" name="QuantityUnitIdentifier" />
       <xs:element type="xs:string" name="MinimumQuantity" />
       <xs:element type="xs:string" name="MaximumQuantity" />
       <xs:element type="xs:string" name="LastUpdate" />
       <xs:element type="xs:string" name="Description" />
       <xs:element type="xs:float" name="PriceInUSD" />
       <xs:element type="xs:float" name="PriceInBRL" />
       <xs:element type="xs:float" name="PriceInCAD" />
       <xs:element type="xs:float" name="PriceInCNY" />
       <xs:element type="xs:float" name="PriceInEUR" />
       <xs:element type="xs:float" name="PriceInJPY" />
       <xs:element type="xs:float" name="PriceInKRW" />
       <xs:element type="xs:float" name="PriceInTWD" />
       <xs:element type="xs:byte" name="Delete" fixed="0"/>
    </xs:sequence>
   </xs:complexType>
 </xs:element>
 </xs:schema>  

我希望价格元素和日期元素可以为空,但“日期”和“浮动”元素出现错误,如下所示

Error - Line 10, 25: org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 25; cvc-datatype-valid.1.2.1: '' is not a valid value for 'date'.
Error - Line 10, 25: org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 25; cvc-type.3.1.3: The value '' of element 'StartDate' is not valid.
Error - Line 11, 21: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 21; cvc-datatype-valid.1.2.1: '' is not a valid value for 'date'.
Error - Line 11, 21: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 21; cvc-type.3.1.3: The value '' of element 'EndDate' is not valid.
Error - Line 18, 27: org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 27; cvc-datatype-valid.1.2.1: '' is not a valid value for 'float'.
Error - Line 18, 27: org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 27; cvc-type.3.1.3: The value '' of element 'PriceInBRL' is not valid.
Error - Line 19, 27: org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 27; cvc-datatype-valid.1.2.1: '' is not a valid value for 'float'.
Error - Line 19, 27: org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 27; cvc-type.3.1.3: The value '' of element 'PriceInCAD' is not valid.
Error - Line 20, 27: org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 27; cvc-datatype-valid.1.2.1: '' is not a valid value for 'float'.
Error - Line 20, 27: org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 27; cvc-type.3.1.3: The value '' of element 'PriceInCNY' is not valid.
Error - Line 21, 27: org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 27; cvc-datatype-valid.1.2.1: '' is not a valid value for 'float'.
Error - Line 21, 27: org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 27; cvc-type.3.1.3: The value '' of element 'PriceInEUR' is not valid.
Error - Line 22, 27: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 27; cvc-datatype-valid.1.2.1: '' is not a valid value for 'float'.
Error - Line 22, 27: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 27; cvc-type.3.1.3: The value '' of element 'PriceInJPY' is not valid.
Error - Line 23, 27: org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 27; cvc-datatype-valid.1.2.1: '' is not a valid value for 'float'.
Error - Line 23, 27: org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 27; cvc-type.3.1.3: The value '' of element 'PriceInKRW' is not valid.
Error - Line 24, 27: org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 27; cvc-datatype-valid.1.2.1: '' is not a valid value for 'float'.
Error - Line 24, 27: org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 27; cvc-type.3.1.3: The value '' of element 'PriceInTWD' is not valid.

我尝试了 xsd nullable 、minOccurs 和 maxOccurs 但我没有成功。任何人都可以说明一下 minOccurs 、 maxOccurs 和 xsd nullable 来解决这个问题吗?谢谢

【问题讨论】:

【参考方案1】:

将 nillability 设置为 true 具有允许空元素的效果,如果它带有属性值规范 xsi:nil="true"(或等效项 - 命名空间前缀可能会有所不同,而值可能是文字 1 true)。但是xsi:nil的默认值是false。

如果您希望在不指定 xsi:nil="true" 的情况下允许该值不存在,一种方法是定义一个联合类型,其第一个成员是 xsd:float(或 xsd:date),其第二个成员是 xsd 的限制:string,其单个值为空字符串。

【讨论】:

以上是关于xsd 浮点数和空值日期的主要内容,如果未能解决你的问题,请参考以下文章

Python输入验证 - 接受正浮点数或int

将日期转换为浮点数以对 Pandas 数据框进行线性回归

Python基础

将日期对象放入 MongoDB,使用 pymongo 查询时返回浮点数

用于存储 Int 浮点数和日期的用户定义数据类型

将字符串、浮点数和日期时间值插入 sql 表