Schema constraints for XML (Java)

Source: Internet
Author: User

1.schema constraints

*DTD Syntax: <! ELEMENT name constraint >

The schema conforms to the XML syntax, which is an XML statement.

There can be multiple schemas in an XML file, multiple schemas use namespaces to differentiate (similar to packages in Java), and only one DTD can be mentioned in an XML file.

There are pcdata types in the DTD, and many data types are supported in the schema, for example, age can only be an integer, and an integer type can be defined directly in the schema.

Schema syntax is more complex and cannot replace DTDs at this time.

Person.xml

<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
<person xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://www.example.org/schema"
xsi:schemalocation= "Http://www.example.org/schema schema.xsd" id1= "123" >
<name> Baojian </name>
<age>20</age>
<school>** University **</school>
</person>

<!--
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance": Indicates a constrained file
Xmlns= "Http://www.example.org/schema": TargetNamespace in the constraint file
: XSi is a casual alias, for the district to be separated by the same name.
xsi:schemalocation= "Http://www.example.org/schema schema.xsd";:
TargetNamespace Space Constraint document path
-

2.schema Quick Start

* Create a schema file whose suffix is. xsd

Schema.xsd

<?xml version= "1.0" encoding= "UTF-8"?>
<schema xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.example.org/schema"
Xmlns:tns= "Http://www.example.org/schema"
elementformdefault= "qualified" >
<element name= "Person" >
<complexType>
<sequence>
<element name= "Name" type= "string" maxoccurs= "unbounded" ></element>
<element name= "age" type= "int" ></element>
<element name= "School" type= "string" ></element>
</sequence>
<attribute name= "ID1" type= "int" use= "required" ></attribute>
</complexType>
</element>
</schema>

<!--constraints
In the schema file:
Property: Xmlns= "Http://www.w3.org/2001/XMLSchema" indicates that the current XML file is a constraint file
Targetnamespace= "Http://www.example.org/schema": When using the schema file, the constraint file is introduced directly through this address.
elementformdefault= "qualified": indicates a good quality.
1. See how many elements are in the XML and how many of them are in the schema file <element></element>
2. Judging simple elements/complex elements
* Complex elements:
<element name= "Person" >
<complexType>
<sequence>
Child elements
</sequence>
</complexType>
</element>
* Simple elements: written in
<sequence>
Simple elements
</sequence>

* * <sequence> indicates that elements must appear in the same order
<all&gt: element can only appear once
<choice>: An element can only appear in one of these
Maxoccurs= "unbounded": Indicates the number of occurrences of an element
<any></any>: Represents any Element * *

Attribute constraints: Can be used only for complex elements, where the position is placed as follows:
</sequence>
<attribute name= "ID1" type= "int" use= "required" ></attribute>
</complexType>
All of the above statements must have a "space" before the property type. If not, there will be an error.
Multiple schema constraints can be used to view documents.
-

Schema constraints for XML (Java)

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.