簡體   English   中英

如何為遵循XML要求編寫xsd

[英]how to write xsd for following XML requirement

我有以下需要XSD的xml。

<?xml version="1.0" encoding="UTF-8"?>
 <Metadata>
  <Field Name="Claim Number">100</Field>
  <Field Name="Audit Year">2010</Field>
  <Field Name="Auditor Name">ABC PQR</Field>
</Metadata>

在上述xml中,如果我刪除<Field Name="Audit Year">2010</Field>這行,則必須說給定XML中不存在“ Audit Year”。

下面是當前的XSD,但這不是正確的方法。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:simpleType name="temp" >
<xs:restriction base="xs:string">
  <xs:enumeration value="Audit Year" />
</xs:restriction>

<xs:element name="Metadata">
<xs:complexType>
  <xs:sequence>
    <xs:element maxOccurs="unbounded" name="Field">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
           <xs:attribute name="Name" type="temp" use="required"/>
           <!--<xs:attribute name="Name" type="xs:string" use="optional" />-->
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType></xs:element></xs:schema>

最簡單的解決方案是將每個Field name="X"元素重命名為X ,因此您的XML是

<Metadata>
  <ClaimNumber>100</ClaimNumber>
  <AuditYear>2010</AuditYear>
  <Auditor>ABC PQR</Auditor>
</Metadata>

現在的任務很簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM