DTD与XSD的一个范例

一个xml的格式如下,


<?xml version="1.0"?>
<!-- 本文档根元素为gridbag, 其中验证文档为gridbag.dtd -->
<!DOCTYPE gridbag SYSTEM "gridbag.dtd">
<gridbag>
<row>
<cell anchor="EAST">
<bean>
<class>javax.swing.JLabel</class>
<property>
<name>text</name>
<value><string>Face:</string></value>
</property>
</bean>
</cell>
<cell fill="HORIZONTAL" weightx="100">
<bean id="face">
<class>javax.swing.JComboBox</class>
</bean>
</cell>
<cell gridheight="4" fill="BOTH" weightx="100" weighty="100">
<bean id="sample">
<class>javax.swing.JTextArea</class>
<property>
<name>text</name>
<value><string>The quick brown fox jumps over the lazy dog</string></value>
</property>
<property>
<name>editable</name>
<value><bolean>false</bolean></value>
</property>
<property>
<name>lineWrap</name>
<value><boolean>true</boolean></value>
</property>
<property>
<name>border</name>
<value>
<bean>
<class>javax.swing.border.EtchedBorder</class>
</bean>
</value>
</property>
</bean>
</cell>
</row>
<row>
<cell gridwidth="2" weighty="100">
<bean id="bold">
<class>javax.swing.JCheckBox</class>
<property>
<name>text</name>
<value><string>Bold</string></value>
</property>
</bean>
</cell>
</row>
<row>
<cell gridwidth="2" weighty="100">
<bean id="italic">
<class>javax.swing.JCheckBox</class>
<property>
<name>text</name>
<value><string>Italic</string></value>
</property>
</bean>
</cell>
</row>
</gridbag>


,对就在的dtd文件格式:

<!ELEMENT gridbag (row)*> <!--根元素是gridbag,可以多个子元素row-->
<!ELEMENT row(cell)*> <!--row下面可以有多个cell子元素 -->
<!ELEMENT cell(bean)> <!--cell元素下只有一个bean元素-->
<!ATTLIST cell gridx CDATA #IMPLIED> <!--cell有gridx属性,类型为任意文本,如果有特殊字符用相应的替代字符代替(用于属性中),可选-->
<!ATTLIST cell gridy CDATA #IMPLIED>
<!ATTLIST cell gridwidth CDATA "1"> <!--gridwidhth属性默认为1-->
<!ATTLIST cell gridheight CDATA "1">
<!ATTLIST cell weightx CDATA "0">
<!ATTLIST cell weighty CDATA "0">
<!ATTLIST cell fill (NODE|BOTH|HORIZONTAL|VERTICAL) "NONE"> <!--fill属性有几个可选值,默认为NONE-->
<!ATTLIST cell anchor (CENTER|NORTH|NORTHEAST|EAST|SOUTHEAST|SOUTH|SOUTHWEST|WEST|NORTHWEST) “CENTER”>
<!ATTLIST cell ipdx CDATA "0">
<!ATTLIST cell ipady CDATA "0">

<!ELEMENT bean (class, property*)> <!--bean子元素有一个class子元素,多个property子元素-->
<!ATTLIST bean id ID #IMPLIED> <!--bean的id属性是ID类型,也就是唯一性 -->

<!ELEMENT class (#PCDATA)> <!--class元素下面可以只包含文本也可以包含子元素,类似于ANY(用于元素中)-->
<!ELEMENT property (name, value)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT value (int|string|boolean|bean)>
<!ELEMENT int (#PCDATA)>
<!ELEMENT string (#PCDATA)>
<!ELEMENT boolean (#PCDATA)>


XSD文件格式如下

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="gridbag" type="GridBagType"/><!--根元素-->

<xsd:element name="bean" type="BeanType"/>

<xsd:complexType name="GridBagType">
<xsd:sequence>
<xsd:element name="row" type="RowType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="RowType">
<xsd:sequence>
<xsd:element name="cell" type="CellType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="CellType">
<xsd:sequence>
<xsd:element ref="bean"></xsd:element>
</xsd:sequence>
<xsd:attribute name="gridx" type="xsd:int" use="optional"/>
<xsd:attribute name="gridy" type="xsd:int" use="optional"></xsd:attribute>
<xsd:attribute name="gridwidth" type="xsd:int" use="optional" default="1"></xsd:attribute>
<xsd:attribute name="gridheight" type="xsd:int" use="optional" default="1"/>
<xsd:attribute name="weightx" type="xsd:int" use="optional" default="0"/>
<xsd:attribute name="weighty" type="xsd:int" use="optional" default="0"/>
<xsd:attribute name="fill" use="optional" default="NONE">
<xsd:simpleType>
<xsd:retriction base="xsd:string">
<xsd:enumeration value="NONE"/>
<xsd:enumeration value="BOTH"/>
<xsd:enumeration value="HORIZONTAL"/>
<xsd:enumeration value="VERTICAL"></xsd:enumeration>
</xsd:retriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="anchor" use="optional" default="CENTER">
<xsd:simpleType>
<xsd:retriction base="xsd:string">
<xsd:enumeration value="CENTER"></xsd:enumeration>
<xsd:enumeration value="NORTH"/>
<!-- 下面的类同 -->
</xsd:retriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="ipady" type="xsd:int" use="optional" default="0"></xsd:attribute>
<xsd:attribute name="ipadx" type="xsd:int" use="optional" default="0"/>
</xsd:complexType>

<xsd:complexType name="BeanType">
<xsd:sequence>
<xsd:element name="class" type="xsd:string"/>
<xsd:element name="property" type="PropertyType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attibute name="id" type="xsd:ID" use="optional"></xsd:attibute>
</xsd:complexType>

<xsd:complexType name="PropertyType">
<xsd:sequence>
<xsd:element name="name" tyype="xsd:string"/>
<xsd:element name="value" type="ValueType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType>
<xsd:choice>
<xsd:element ref="bean"/>
<xsd:element name="int" type="xsd:int"/>
<xsd:element name="string" type="xsd:string"/>
<xsd:element name="boolean" type="xsd:boolean"/>
</xsd:choice>
</xsd:complexType>
</xsd:schema>


DTD比XSD简单,功能上也相对较弱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值