DTD and schema constraints for XML

Source: Internet
Author: User

Why use Constraints?

XML is a custom label, and sometimes tags are too many to remember, so you need to have constraints to tell me which tags I can write, which labels are wrong


What kinds of constraints are in XML?

There are many constraints in which DTD and schema constraints are most common.


What is the constraint in essence?

A constraint is essentially an XML file.


The difference between DTD constraints and schema constraintsCharacteristics of the difference:

DTD constraints are old, simple, and some old frameworks use DTDs as constraints: Both Struts2 and hibernate use DTDs as constraints to their XML configuration files

Schema Constraints are more powerful and more extensive, and both Tomcat and spring use schemas as constraints on their XML configuration files.


The difference between files:

DTD constraint is already. DTD is named suffix

schema constraint has. xsd named suffix


Grammatical differences: Basic syntax for DTD constraints:

defining elements: <! Element name Usage Rules >

defining attributes: <! Attlist element Name Property Name property value type setting description >

Basic syntax for schema constraints:

The writing specification is the same as an XML file that contains a relationship

root node is: <xs:schema xmlns:xs= "namespace" >

The following attribute nodes and ELEMENT nodes, as well as text nodes, are nested according to the needs of the use, that is, how to nest in the actual use when writing schema files

element node: <xs:element name= "Name" ></xs:element>

Attribute node: <xs:attribute name= "name" type= "type" use= "Usage Requirements"/>




Why do you learn DTD and schema constraints?

Not to write a. dtd or. xsd file!

But in order to know the concept of constraint, it will be able to write XML more handy!


How does the DTD constraint be introduced in XML?

DTD document on-premises : <! DOCTYPE root element SYSTEM "DTD document Path" >

DTD document is not local : <! DOCTYPE root element Public "DTD name" "DTD document URL" >

Demo: (from Struts2 's Struts.xml)

<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/dtds/ Struts-2.0.dtd ">


How does XML introduce schema constraints?

1. View the schema document, find the root element, write it out in XML

2. The name of the root element from which namespace. Use the xmlns directive to declare

3. Which XSD file does the introduced namespace correspond to? Use schemalocation to specify: Two values: The first is the path to the second XSD file for the namespace

Demo: (Web. XML from Tomcat)

<web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" V ersion= "3.0" >



How do I use constraints if I can't connect to the Internet during development?

Map The URI of the constraint to local in the IDE


DTD and schema constraints for XML

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.