【问题标题】:how to xml schema using xsd validator for inner text?如何使用 xsd 验证器对内部文本进行 xml 架构?
【发布时间】:2014-03-14 14:37:26
【问题描述】:

我有一个 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>

现在必须在xsd中添加什么属性才能使ticket类型的内部文本为空

【问题讨论】:

    标签: xml-parsing xsd-validation


    【解决方案1】:

    您可以将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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 2012-09-16
      • 1970-01-01
      • 2011-06-18
      • 2012-04-13
      • 2014-01-22
      • 1970-01-01
      相关资源
      最近更新 更多