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

黄舟
黄舟Original
2017-03-10 19:28:511724browse

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">

Example

The following example shows how to use the schema:

  
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  
      
          
              
              
              
          
      
  

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 6f87483d610e6467d66b50e3582f428d 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!

Statement:
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