【发布时间】:2016-03-07 05:56:10
【问题描述】:
我在针对包含以下定义的 XSD 验证空元素时遇到问题。如果 total_amt 为空,我们希望 XML 文档中有一个空元素,例如
<total_amt/>
所以我在 XSD 中创建了一个自定义数据类型,如下所示:
<xs:simpleType name="decimal-or-empty">
<xs:union memberTypes="xs:decimal empty-string" />
</xs:simpleType>
<xs:simpleType name="empty-string">
<xs:restriction base="xs:string">
<xs:enumeration value="" />
</xs:restriction>
</xs:simpleType>
并且元素的XSD定义如下所示。
<xs:element name = "total_amt"
nillable="false"
minOccurs="0"
type="decimal-or-empty">
<xs:complexType>
<xs:SimpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="precision"
type="xs:integer"/>
</xs:extension>
</xs:SimpleContent>
</xs:complexType>
</xs:element>
如果我删除了上述 XSD 中的精度属性,那么它会验证自定义数据类型,并且它适用于空值
<xs:element name = "total_amt"
nillable="false"
minOccurs="0"
type="decimal-or-empty">
</xs:element>
但是当我定义precision属性时,结果总是XML实例无效。
那么,谁能帮我编写 XSD,我可以在其中放置 precision 属性并在 total_amt 的值为空时仍然验证 XML?
【问题讨论】:
标签: xml xsd xsd-validation