如何在xml中应用doctype?
<?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?
答案 0 :(得分:1)
尝试以下架构(您需要填充类型定义):
<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>
然后,您希望验证的XML实例可以通过在根节点中包含xmlns属性与架构相关联:
<application xmlns="http://example.com/sample">
<settings>
....
</settings>
<environment name="production">
....
</environment>
<environment name="development">
....
</environment>
</application>