强制使用 xsd 元素 [英] Make an xsd element mandatory
问题描述
我有一个 xml 文件,我正在尝试根据 xsd 文件对其进行验证.工作正常.我想修改 xsd 以强制使用元素值.如果元素 FirstName
为空 (<FirstName></FirstName>
)>
xml 文件
xsd 文件
Mandatory
属性 minOccurs 可选.指定 any 元素可以在父元素中出现的最小次数.该值可以是任何 >= 0 的数字.默认值为 1.(默认是强制的)
<xs:element name="lastName" type="xs:string"/><!-- 强制错误--><xs:element name="lastName" type="xs:string" minOccurs="0"/>
非空
I have an xml file that I am trying to validate against an xsd file. Works fine. I want to modify the xsd to make an element value mandatory. How do I do this so when I validate the xml file I want it to fail if the element FirstName
is blank (<FirstName></FirstName>
)
xml File
<?xml version="1.0" encoding="utf-8" ?>
<Patient>
<FirstName>Patient First</FirstName>
<LastName>Patient Last</LastName>
</Patient>
xsd File
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Patient">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Mandatory
Attribute minOccurs Optional. Specifies the minimum number of times the any element can occur in the parent element. The value can be any number >= 0. Default value is 1. (By default is mandatory)
<!-- mandatory true-->
<xs:element name="lastName" type="xs:string" />
<!-- mandatory false-->
<xs:element name="lastName" type="xs:string" minOccurs="0" />
Not Empty
<xs:element name="lastName" type="xs:string" >
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
这篇关于强制使用 xsd 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!