English 中文(简体)
XML Schema和DTD之间有什么区别?
原标题:What is difference between XML Schema and DTD?
  • 时间:2009-10-09 14:39:03
  •  标签:

我 question开了这个问题,但我不清楚XML的图象和DTD(文件类型定义)是什么,以及为什么XML的表象比DTD更为强大。

任何指导都将受到高度赞赏。

最佳回答

http://www.informit.com/articles/article.aspx?p=24614&seqNum=3: rel=“noreferer” Diss between DTDs and Schema section of the Converting a DTD into a Schema 条款:

The critical difference between DTDs and XML Schema is that XML Schema utilize an XML-based syntax, whereas DTDs have a unique syntax held over from SGML DTDs. Although DTDs are often criticized because of this need to learn a new syntax, the syntax itself is quite terse. The opposite is true for XML Schema, which are verbose, but also make use of tags and XML so that authors of XML should find the syntax of XML Schema less intimidating.

The goal of DTDs was to retain a level of compatibility with SGML for applications that might want to convert SGML DTDs into XML DTDs. However, in keeping with one of the goals of XML, "terseness in XML markup is of minimal importance," there is no real concern with keeping the syntax brief.

[......]

因此,当我们皈依性传播疾病时,其他一些可能特别重要的差异是什么? 让我们看一看。

<><>Typing

DTDs和XML Schema之间最重要的区别是,在Schema与要素和属性声明一起创建和使用数据类型的能力。 事实上,XML Schema建议的一半用于数据搭配和XML Schema,这产生了如此重要的差别。 我们详细介绍了本书第三部分“XML Schema数据类型”中的数据类型。

[......]

<>可见>

DTDs和Schema差别很大的另一个领域是出现限制。 如果你回顾一下我们在第2章“结构”中以前的例子(或你本人与TDD公司合作),那么你可以用三种符号来限制某一要素的发生次数:*、+和?

[......]

<>0>

因此,我要说的是,我们有了一个因素,我们希望能够确定锡土的大小特征,使用户能够选择一个大小:小、中或大的大小。 我们的残疾人发展基金会希望:

<!ELEMENT item (shirt)>
<!ELEMENT shirt (#PCDATA)>
<!ATTLIST shirt
    size_value (small | medium | large)>

[......]

但是,如果我们希望<条码>将变成一个要素? 我们能够用DVD来做到这一点。 DTDs没有规定在内容内容中进行列举。 然而,由于与Schema的数据类型,当我们宣布了前一个例子中的列举时,我们实际上创建了一个名为“<条码>的<传真/<代码>的<代码>。 我们现在可以用一个要素:

<xs:element name="size" type="size_value">

[......]

问题回答

XML Schema定义与文件类型定义之间的差别包括:

  • XML schemas are written in XML while DTD are derived from SGML syntax.
  • XML schemas define datatypes for elements and attributes while DTD doesn t support datatypes.
  • XML schemas allow support for namespaces while DTD does not.
  • XML schemas define number and order of child elements, while DTD does not.
  • XML schemas can be manipulated on your own with XML DOM but it is not possible in case of DTD.
  • using XML schema user need not to learn a new language but working with DTD is difficult for a user.
  • XML schema provides secure data communication i.e sender can describe the data in a way that receiver will understand, but in case of DTD data can be misunderstood by the receiver.
  • XML schemas are extensible while DTD is not extensible.

并非所有这些子弹点都是100%的准确点,但你们都获得了光顾。

另一方面:

  • DTD lets you define new ENTITY values for use in your XML file.
  • DTD lets you extend it local to an individual XML file.

正如许多人以前提到的那样,XML Schema利用以XML为基础的辛塔和DTDs有一个独特的辛迪加。 DTD并不支持数据类型,这确实很重要。

让我们看到一个非常简单的例子,即大学有多个学生,每个学生有两个要素“名称”和“年”。 请注意,我在守则中仅作评论就使用了“/->”;

“在座的影像描述”/

现在,我将在发司和社发司写这一榜样。

<><>DTD

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE university[              // --> university as root element 
<!ELEMENT university (student*)>   // --> university has  * = Multiple students
<!ELEMENT student (name,year)>     // --> Student has elements name and year
<!ELEMENT name (#PCDATA)>          // --> name as Parsed character data
<!ELEMENT year (#PCDATA)>          // --> year as Parsed character data
]>

<university>
    <student>
        <name>
            John Niel             //---> I can also use an Integer,not good
        </name>
        <year>
            2000                 //---> I can also use a string,not good
        </year>
    </student>
</university>

<XML Schema定义(XSD)

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:complexType name ="uniType">                    //--> complex datatype uniType
 <xsd:sequence>
  <xsd:element ref="student" maxOccurs="unbounded"/> //--> has unbounded no.of students
 </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="stuType">                     //--> complex datatype stuType
 <xsd:sequence>
  <xsd:element ref="name"/>                          //--> has element name
  <xsd:element ref="year"/>                          //--> has element year
 </xsd:sequence>
</xsd:complexType>

<xsd:element name="university" type="uniType"/>       //--> university of type UniType 
<xsd:element name="student" type="stuType"/>          //--> student of type stuType
<xsd:element name="name" type="xsd:string"/>          //--> name of datatype string
<xsd:element name="year" type="xsd:integer"/>         //--> year of datatype integer
</xsd:schema>



<?xml version="1.0" encoding="UTF-8"?>
<university>
    <student>
        <name>
            John Niel          
        </name>
        <year>
            2000                      //--> only an Integer value is allowed
        </year>
    </student>
</university>

DTD预设XML,因此不具有XML的效力。 这很可能是XSD发明的最大原因。

Similarities between XSD and DTD

both specify elements, attributes, nesting, ordering, #occurences

Differences between XSD and DTD

XSD also has data types, (typed) pointers, namespaces, keys and more.... unlike DTD 

此外,虽然科学、技术和科学部的辛勤工作很少,但XML是延伸的,因此可以迅速学习。

一种不同之处是,在DVD中,某一要素的内容模式完全由其名称决定,而不论该文件中的内容如何:

假设你想要

  • a person element
  • with a child element called name
  • an name itself has child elements first and last.

   <person>
       <name>
            <first></first>
            <last></last>
       </name>
   </person>

如果同一文件中的<条码>、要素也需要有儿童要素名称,则DTD要求这一名称要素也必须有儿童要素<条码>、第1条和<条码>。 尽管city.name 并不要求firstlast作为儿童。

相比之下,XML Schema允许你在当地宣布儿童元素类型;你可以分别宣布<> 姓名/编码> 和<代码> 姓名/编码> 和<> 姓名/编码> 。 因此,在这些情况下给他们适当的内容模式。

另一个重大差异是支持名称空间。 由于DTDs是最初的XML规格的一部分(和从SGML中继承),因此,由于XML名称空间后来被具体指明,因此它们根本不使用名称。 你们可以结合名称空间使用DVD,但需要一些胁迫,如被迫界定性病的预设点,只使用这些预设物,而不是使用任意的预设物。

对我来说,其他差异大多是表面的。 数据类型的支持可以很容易地添加到DTDs上,而Syntax只是yn。 (一) 发现XML Schema syntax horrible,并且永远不想交出XML Schema,我想说这句话是关于DTDs或RELAX NG的chem;如果我需要XML Schema,出于某种原因,我通常写的是RELAX NG,并将之改成<>trang>/code>。)

<>Similarities

DTDs和Schemas都履行同样的基本职能:

  • First, they both declare a laundry list of elements and attributes.
  • Second, both describe how those elements are grouped, nested or used within the XML. In other words, they declare the rules by which you are allowing someone to create an XML file within your workflow, and
  • Third, both DTDs and schemas provide methods for restricting, or forcing, the type or format of an element. For example, within the DTD or Schema you can force a date field to be written as 01/05/06 or 1/5/2006.

<>Differences:

  • 用于文本密集型应用的技术性影响比较好,而表象在数据密集型工作流程方面有若干优势。

  • Schemas是用XML书写的,因此遵循同样的规则,而DTDs是用完全不同的语言书写的。

<>>Examples:

DTD:

<?xml version="1.0" encoding="UTF-8"?>
    <!ELEMENT employees (Efirstname, Elastname, Etitle, Ephone, Eemail)>
         <!ELEMENT Efirstname (#PCDATA)>
         <!ELEMENT Elastname (#PCDATA)>
         <!ELEMENT Etitle (#PCDATA)>
         <!ELEMENT Ephone (#PCDATA)>
         <!ELEMENT Eemail (#PCDATA)>

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">
<xsd:element name="dataroot">
     <xsd:complexType>
          <xsd:sequence>
               <xsd:element ref="employees" minOccurs="0" maxOccurs="unbounded"/>
          </xsd:sequence>
          <xsd:attribute name="generated" type="xsd:dateTime"/>
      </xsd:complexType>
</xsd:element>
<xsd:element name="employees">
      <xsd:annotation>
           <xsd:appinfo>
               <od:index index-name="PrimaryKey" index-key="Employeeid " primary="yes"
                unique="yes" clustered="no"/>
          <od:index index-name="Employeeid" index-key="Employeeid " primary="no" unique="no"
           clustered="no"/>
     </xsd:appinfo>
</xsd:annotation>
     <xsd:complexType>
          <xsd:sequence>
               <xsd:element name="Elastname" minOccurs="0" od:jetType="text"
                od:sqlSType="nvarchar">
                    <xsd:simpleType>
                         <xsd:restriction base="xsd:string">
                              <xsd:maxLength value="50"/>
                         </xsd:restriction>
                    </xsd:simpleType>
               </xsd:element>
               <xsd:element name="Etitle" minOccurs="0" od:jetType="text" od:sqlSType="nvarchar">
                    <xsd:simpleType>
                         <xsd:restriction base="xsd:string">
                              <xsd:maxLength value="50"/>
                         </xsd:restriction>
                    </xsd:simpleType>
               </xsd:element>
               <xsd:element name="Ephone" minOccurs="0" od:jetType="text"
                od:sqlSType="nvarchar">
                    <xsd:simpleType>
                         <xsd:restriction base="xsd:string">
                              <xsd:maxLength value="50"/>
                         </xsd:restriction>
                    </xsd:simpleType>
               </xsd:element>
               <xsd:element name="Eemail" minOccurs="0" od:jetType="text"
               od:sqlSType="nvarchar">
                    <xsd:simpleType>
                         <xsd:restriction base="xsd:string">
                              <xsd:maxLength value="50"/>
                         </xsd:restriction>
                    </xsd:simpleType>
               </xsd:element>
               <xsd:element name="Ephoto" minOccurs="0" od:jetType="text"
                od:sqlSType="nvarchar">
                    <xsd:simpleType>
                         <xsd:restriction base="xsd:string">
                              <xsd:maxLength value="50"/>
                         </xsd:restriction>
                    </xsd:simpleType>
               </xsd:element>
          </xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

XML DTD

<>DTD的目的是界定XML文件的结构。 该法律界定了法律要素清单的结构:

<!ATTLIST contact type CDATA #IMPLIED>
<!ELEMENT address1 ( #PCDATA)>
<!ELEMENT city ( #PCDATA)>
<!ELEMENT state ( #PCDATA)>
<!ELEMENT zip ( #PCDATA)>

XML Schema

XML Schema enables schema authors to specify that element quantity’s data must be numeric or, even more specifically, an integer. In the following example I used string:

<xs:element name="note">
<xs:complexType>
  <xs:sequence>
    <xs:element name="address1" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="state" type="xs:string"/>
    <xs:element name="zip" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

DTD只能有两种类型的数据:CDATA和PCDATA。 但是,在图表中,你可以使用你在编制方案时所用的所有原始数据类型,而且你可以灵活地界定自己的习惯数据类型。

开发商根据核心数据类型,使用不同的操作者和变压器,可以生成定制数据类型。

当XML首次出来时,我们被告知它将解决我们的所有问题: XML将是方便用户的,可以肯定地挖掘,避免强硬,不需要任何方案拟订技能。 我学习了DVD,并写了我自己的XML教区。 15年之后,我看到,大多数XML不便于使用,而且不十分合理(取决于其使用)。 一旦一些cl子把XML打到一个数据库,我就知道数据类型是完全不可避免的。 而且,你应当看到,我不得不在另一天工作。 如果是这样,我不知道什么! 现在,看到与XML数据或接口相关的各类问题都变得不寻常。 我热爱XML,但远未达到其最初的入侵起点。

答案是否简短? 由于科学、科学、技术和革新司允许你更精确地界定XML结构,因此DTDs被贬为XSD。

DTD被大量折旧,因为它作为化物语言的效用有限,没有支持名称空间,也没有支持数据类型。 此外,DTD syntax非常复杂,难以理解和维持。





相关问题
热门标签