


Detailed explanation of XSD sample code for schema definition in XML programming
Mar 10, 2017 pm 07:28 PMThis article mainly introduces the detailed explanation of schema definition XSD in XML programming, and explains how to declare schema and define types in XML documents. Friends in need can refer to it
XML schema is usually called For XML Schema Definition (XSD). It is used to describe and validate the structure and content of XML data. XML schemas define elements, attributes and data types. Schema elements also support namespaces. It is similar to a database schema that describes the data in a database.
Syntax
We need to declare the schema in the XML document as follows:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
Example
The following example shows how to use the schema:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="contact"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="company" type="xs:string" /> <xs:element name="phone" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
The basic idea behind XML schema is to describe the legal format that an XML document can accept.
Elements
As we saw in the XML Elements chapter, elements are the building blocks of XML documents. Elements can be defined within the XSD as follows:
<xs:element name="x" type="y"/>
Defining types
We can define XML schema elements as follows:
Simple types: Simple type elements can only be used in textual contexts. Some predefined simple types are: xs:integer, xs:boolean, xs:string, xs:data. For example:
<xs:element name="phone_number" type="xs:int" />
Complex type: A complex type is a container defined by other elements. Allows us to specify which element can contain child elements to provide some structure to the XML document. For example:
<xs:element name="Address"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="company" type="xs:string" /> <xs:element name="phone" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:element>
In the above example, the Address element is composed of child elements. It is a container for other <xs:element> definitions, allowing us to build a simple hierarchy of elements in an XML document.
Global types: For global types, we can define independent types in the document, and it can also use all other references. For example, let's say we want to generalize person and company for different company addresses. In this case, we can define a generic type like this:
<xs:element name="AddressType"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="company" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element>
and then use this type in the following example:
<xs:element name="Address1"> <xs:complexType> <xs:sequence> <xs:element name="address" type="AddressType" /> <xs:element name="phone1" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Address2"> <xs:complexType> <xs:sequence> <xs:element name="address" type="AddressType" /> <xs:element name="phone2" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:element>
No longer need to define name and compacty twice (once for Address1 and once for Address2), now we have an independent definition. This makes maintenance easier, for example, if we decide to add a "Postcode" element to an address, we only need to add it in one place.
Attributes
Attributes in XSD provide additional information about the element. The attribute with name and type attributes is as follows:
<xs:attribute name="x" type="y"/>
The above is the detailed content of Detailed explanation of XSD sample code for schema definition in XML programming. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can I open an XML file using PowerPoint?

Filtering and sorting XML data using Python

Convert XML data to CSV format in Python

Python implements conversion between XML and JSON

Using Python to merge and deduplicate XML data

Handling errors and exceptions in XML using Python

Import XML data into database using PHP

Use PHP and XML to process and display geolocation and map data
