XSD and DTD in XML and the use of standalone

Source: Internet
Author: User
Tags xml file example

About XSD and DTD in XML structure and the use of standalone

XmlDeclaration declare= document. Createxmldeclaration ("1.0", "Utf-8", "yes"); Yes is the value of standalone,

There is a standalone in the declaration in XML

Standalone is used to indicate whether the file calls other external files. A value of "yes" indicates that no external file is being called, and if the value is "no" then there is a call to the external file. The default value is "Yes".

What is the difference between an external file referred to here or a search for XML as a valid constraint file, or a DTD or schema?

Although XML1.0 provides a mechanism for document type definition (DTD) to standardize the formatting rules of XML. But it is inherently flawed, for example, by using non-XML syntax rules, not supporting more data types, extensibility

, in order to overcome these shortcomings, XML Schema appears. It is recommended to use XML schemas instead of DTDs in XML.

XML Schemas (schemas), like DTDs, are also used to define constraints on XML documents. But unlike DTDs, XML schemas follow XML syntax rules to better support data types and namespaces.

"The XML Schema is a XML based alternative to DTD," said the consortium. Schemas are defined in a separate file, typically with an. xsd extension. Each schema definition has a schema for the root element that belongs to the name null

Room The schema element can contain optional properties.

Like what:

<xs:schema Xmlns:xs=http://www.w3.org/2001/xmlschema elementformdefault= "qualified" attributeFormDefault= " Unqualified ">

This indicates that the elements used in the pattern come from the namespace.

The XML file is linked to the corresponding pattern using the schemalocation attribute in the schema namespace. The schema namespace must be defined using the SchemaLocation property.

All of these definitions appear in the root element of the XML document.

The syntax is as follows:

<root_element schema_namespace_definition schema_location_definition>

Here is an example:

<books xmlns:xs= "books.xsd" >

Let's take a look at an XML file example (Message.xml) that describes the file with a DTD and XML Schema, to see the difference between them:

1. Using DTDs:

    1. <?xml version= "1.0" standalone= "yes"?>
    2. <! DOCTYPE message [
    3. <! ELEMENT message (To,from,body) >
    4. <! ELEMENT to (#PCDATA) >
    5. <! ELEMENT from (#PCDATA) >
    6. <! ELEMENT Body (#PCDATA) >
    7. ] >

2. The DTD structure definition of message:

    1. <message>
    2. <to>rose</to>
    3. <from>alex</from>
    4. <body>hi,my girl!</body>
    5. </message>

3. Use XML Schema:

Message.xml:

    1. <?xml version= "1.0" standalone= "no"? >
    2. <message xmlns=http://www.example.com xmlns:xsi=http://www.w3.org/2001/xmlschema-instance xsi:schemaLocation= " Message.xsd ">
    3. <to>Rose</to>
    4. <from>Alex</from>
    5. <body>hi,my girl!</body>
    6. </message>

4. The XSD XML schema structure of message is defined:

Message.xsd:

    1. <?xml version= "1.0"?>
    2. <xs:schema xmlns:xs=http://www.w3.org/2001/xmlschema targetnamespace=http://www.example.com xmlns= "" elementformdefault= "qualified" >
    3. <xs:element name= "Message" >
    4. <xs:complexType>
    5. <xs:sequence>
    6. <xs:element name= "to" type= "xs:string"/>
    7. <xs:element name= "from" type= "xs:string"/>
    8. <xs:element name= "Body" type= "xs:string"/>
    9. </xs:sequence>
    10. </xs:complexType>
    11. </xs:element>
    12. </xs:schema>

XSD and DTD in XML and the use of standalone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.