如何使用xsd验证器为内部文本的xml架构?

时间:2014-02-15 02:59:24

标签: xml-parsing xsd-validation

我有一个带有架构的xml文件,如下所示。我生成了一个xsd验证文件来验证。现在我想添加一个验证,即元素Tickettype的内部文本可以为空。我怎么能这样做?

<?xml version="1.0" encoding="utf-8" ?>
<AppStatusDetails>
  <Patronid>G5032788W</Patronid>
  <PatronidType>1</PatronidType>
  <Birthdate>19870716</Birthdate>
  <Tickettype>49</Tickettype>
</AppStatusDetails>

我有一个xsd验证程序文件,如下所示验证xml架构

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="AppStatusDetails">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Patronid" type="xs:string" />
        <xs:element name="PatronidType" type="xs:unsignedByte" />
        <xs:element name="Birthdate" type="xs:unsignedInt" />
        <xs:element name="Tickettype" type="xs:unsignedByte" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

现在attribuet shoudi必须添加到xsd以使票证类型的内部文本为空

1 个答案:

答案 0 :(得分:0)

您可以将nillable="true"添加到XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="AppStatusDetails">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Patronid" type="xs:string" />
        <xs:element name="PatronidType" type="xs:unsignedByte" />
        <xs:element name="Birthdate" type="xs:unsignedInt" />
        <xs:element name="Tickettype" type="xs:unsignedByte" nillable="true"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

然后在文档实例中xsi:nil="true"Tickettype

<?xml version="1.0" encoding="utf-8" ?>
<AppStatusDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Patronid>G5032788W</Patronid>
  <PatronidType>1</PatronidType>
  <Birthdate>19870716</Birthdate>
  <Tickettype xsi:nil="true"></Tickettype>
</AppStatusDetails>