Home Backend Development XML/RSS Tutorial Detailed explanation of XSD sample code for schema definition in XML programming

Detailed explanation of XSD sample code for schema definition in XML programming

Mar 10, 2017 pm 07:28 PM
xml xsd

This 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">
Copy after login

Example

The following example shows how to use the schema:

<?xml version="1.0" encoding="UTF-8"?>  
&lt;xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;  
<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>
Copy after login

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:

&lt;xs:element name=&quot;x&quot; type=&quot;y&quot;/&gt;
Copy after login

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:

&lt;xs:element name=&quot;phone_number&quot; type=&quot;xs:int&quot; /&gt;
Copy after login

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:

&lt;xs:element name=&quot;Address&quot;&gt;  
    &lt;xs:complexType&gt;  
        &lt;xs:sequence&gt;  
            &lt;xs:element name=&quot;name&quot; type=&quot;xs:string&quot; /&gt;  
            &lt;xs:element name=&quot;company&quot; type=&quot;xs:string&quot; /&gt;  
            &lt;xs:element name=&quot;phone&quot; type=&quot;xs:int&quot; /&gt;  
        &lt;/xs:sequence&gt;  
    &lt;/xs:complexType&gt;  
&lt;/xs:element&gt;
Copy after login

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:

&lt;xs:element name=&quot;AddressType&quot;&gt;  
    &lt;xs:complexType&gt;  
        &lt;xs:sequence&gt;  
            &lt;xs:element name=&quot;name&quot; type=&quot;xs:string&quot; /&gt;  
            &lt;xs:element name=&quot;company&quot; type=&quot;xs:string&quot; /&gt;  
        &lt;/xs:sequence&gt;  
    &lt;/xs:complexType&gt;  
&lt;/xs:element&gt;
Copy after login

and then use this type in the following example:

&lt;xs:element name=&quot;Address1&quot;&gt;  
    &lt;xs:complexType&gt;  
        &lt;xs:sequence&gt;  
            &lt;xs:element name=&quot;address&quot; type=&quot;AddressType&quot; /&gt;  
            &lt;xs:element name=&quot;phone1&quot; type=&quot;xs:int&quot; /&gt;  
        &lt;/xs:sequence&gt;  
    &lt;/xs:complexType&gt;  
&lt;/xs:element&gt;  
&lt;xs:element name=&quot;Address2&quot;&gt;  
    &lt;xs:complexType&gt;  
        &lt;xs:sequence&gt;  
            &lt;xs:element name=&quot;address&quot; type=&quot;AddressType&quot; /&gt;  
            &lt;xs:element name=&quot;phone2&quot; type=&quot;xs:int&quot; /&gt;  
        &lt;/xs:sequence&gt;  
    &lt;/xs:complexType&gt;  
&lt;/xs:element&gt;
Copy after login

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:

&lt;xs:attribute name=&quot;x&quot; type=&quot;y&quot;/&gt;
Copy after login


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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can I open an XML file using PowerPoint?

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Filtering and sorting XML data using Python

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data to CSV format in Python

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling errors and exceptions in XML using Python

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Import XML data into database using PHP

Use PHP and XML to process and display geolocation and map data Use PHP and XML to process and display geolocation and map data Aug 01, 2023 am 08:37 AM

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

See all articles