簡體   English   中英

XML Doctype,如何申請

[英]XML Doctype, how to apply

如何在xml中應用文檔類型?

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE application [
       <!ELEMENT application (#PCDATA)>
]>
<application>
       <settings>
              <environment use="production" />
              <database datasource="MySQL" environment="production" />
              <database datasource="MySQL" environment="development" />
              <import>
                     <path value="Application" />
                     <path value="Application/Library" />
              </import>
       </settings>
       <environment name="production">
              <database>
                     <hostname value="127.0.0.1" />
                     <username value="root" />
                     <password value="" />
                     <database value="app" />
              </database>
       </environment>
       <environment name="development">
              <database>
                     <hostname value="127.0.0.1" />
                     <username value="root" />
                     <password value="" />
                     <database value="app" />
              </database>
       </environment>
</application>

我還想驗證XML中必須包含的元素和屬性,例如環境設置,您必須具有此節點,以及如何創建可以由各種XML實現的單個doctype,因此如何驗證。我使用XSD文件驗證XML?

嘗試以下模式(您需要填充類型定義):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://example.com/sample"
    xmlns="http://example.com/sample">
  <xs:element name="application">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="settings" type="settingsType" />
        <xs:element name="environment" maxOccurs="unbounded" type="environmentType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!-- type definitions -->
  <xs:complexType name="settingsType">
    ... define type here...
  </xs:complexType>
  <xs:complexType name="environmentType">
    ... define type here...
  </xs:complexType>
</xs:schema>

然后,通過在根節點中包含xmlns屬性,可以將要驗證的XML實例與模式關聯:

<application xmlns="http://example.com/sample">
       <settings>
              ....
       </settings>
       <environment name="production">
              ....
       </environment>
       <environment name="development">
              ....
       </environment>
</application>

暫無
暫無

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

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