This article mainly introduces the Schema Definition XSD in XML programming and explains how to declare the schema and definition type in XML documents, for more information about schema definition in XML programming, see the next article.
XML Schema is usually called XML Schema Definition (XSD ). It is used to describe and verify the structure and content of XML data. XML Schema defines elements, attributes, and data types. The schema element also supports namespaces. It is similar to the database mode that describes data in a database.
Syntax
We need to declare the schema in the XML document as follows:
Example
The following example shows how to use the mode:
The basic idea behind the XML pattern is to describe the legal format acceptable to the XML document.
Element
As we can see in the XML element chapter, the element is the XML document building block. In XSD, elements can be defined as follows:
Define Type
We can define XML schema elements as follows:
Simple type: simple elements can only be used in text context. Some predefined simple types include: xs: integer, xs: boolean, xs: string, xs: data. For example:
Complex type: the complex type is the container defined by other elements. Allow us to specify which element can contain child elements to provide some structure for the XML document. For example:
In the preceding example, the Address element consists of child elements. It is other The defined container allows us to build a simple hierarchical element in the XML document.
Global type: for the global type, we can define an independent type in the document, it can also use all other references. For example, suppose we want to summarize the person and company addresses for different companies. In this case, we can define a general type as follows:
Use this type in the following example:
You no longer need to define name and compacny twice (Address1 and Address2 at a time). now we have an independent definition. This makes maintenance easier. for example, if we decide to add a "Postcode" element to the address, we only need to add it in one place.
Attribute
Attributes in XSD provide additional information about elements. Attributes with the name and type attributes are as follows:
The above is a detailed description of the sample code for Schema Definition XSD in XML programming. For more information, see other related articles in the first PHP community!