Valid XML documents – Schema and display of XML documents using CSS

Table of Contents

1. Basic structure of XML Schema

1. XML Schema document example

2. Schema basic content map

3. The role of Schema

4. Schema examples

2. Use CSS to display XML documents

1. What is CSS?

2. What are the advantages of using CSS to display XML data?

3. Summary of commonly used CSS

4. CSS definition style

5. Combination of XML and CSS

6. CSS display rules


XML Schema is an XML language used to describe and constrain XML documents. Functionally, it is very similar to DTD, but it is more powerful than DTD. XML Schema is responsible for defining and describing the structure and content schema of XML documents. It can not only define the relationship between elements in XML documents, but also define the data types of elements and attributes. With its ability to enforce document content and structure, XML Schema is an important and powerful new standard in the XML world.

1. Basic structure of XML Schema

1. XML Schema document example

XML Schema document is a special XML document that must follow the syntax rules of XML. It has the same structure as an ordinary XML document. But XML Schema is a text file independent of the XML document, with the extension .xsd. The basic structure of an XMLSchema document is as follows.

<?xml version="1.0" encoding="GB2312"?>
<xsd:schema name="slschema" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
.....
</xsd:schema>

2. Schema basic content map

2.1 Element declaration

Elements are the most important text data in XML Schema. An XML document may not contain any attributes or text data, but it must contain elements (at least one root element). In fact, the data types in XML Schema are mainly for XML elements. , specifically, for the content of various elements and their structure.

The main purpose of XML Schema is to constrain tags in XML files. It uses elements to constrain tags in XML. XML Schema uses element elements to define elements in XML documents. The specific syntax format is as follows.

<element name="Element name" type="Data type" minOccurs="int" maxOccurs="int">

2.2 Property declaration

In XML documents, you can add a variety of attributes to elements to provide additional information. There is a set of syntax for declaring attributes on elements. However, there are two points to note: first, only complex type elements can have them, simple type elements have no attributes; second, elements can have simple types or complex types, but attributes can only have simple types.
The syntax of attribute declaration is as follows.

<element name="element_name" type="dataType">
<xsd:complexType name="dataType">
<xsd:attribute name="attribute_name" type="simple _type" use="use_method" default="value" fixed="value">
</xsd:attribute>
</xsd.complexType>

3. The role of Schema

XML Schema and DTD have the same function. They are both schemas used to define the structure of an XML document. So why do we need XML Schema when we have a DTD? Because XML Schema is more powerful than DTD.

Advantages of Xml Schema over DTD:
(1) The model is extensible
(2) Patterns are richer and more useful than DTDs
(3) The schema is written in XML
(4) Schema supports data types
(5) Schema supports namespace
(6) No need to learn other languages
(7) You can directly use the XML editor to write XML Schema
(8) XML parser can be used directly to parse XML Schema
(9) XML DOM can be used to flexibly operate XML Schema
(10) XSLT technology can be used to convert XML Schema

4. Schema instance

The first one: User.xml document structure

<?xml version="1.0"? >
<user list>
    <user>
         <username>xx</username>
         <password>123456</password>
         <User Type>1</User Type>
   </user>
</user list>

Second: Use global definition Schema, User.xsd

<?xml version='1.0' encoding='utf-8'?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefualt='unqualified'>
 <xs:element name='Userlist' type='userlist'/>
 <xs:complexType name='userlist'><!-- Use complexType to declare the type as a composite type element -->
  <xs:sequence><!-- Use sequence to indicate that the following elements must be displayed in order in the XML document -->
   <xs:element name='user' type='user'/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name='user'>
  <xs:sequence>
   <xs:element name='username' type='user'/>
   <xs:element name='password' type='user'/>
   <xs:element name='user type' type='user'/>
  </xs:sequence>
 </xs:complexType>
</xs:schema>

The second one defines User.xsd to verify the legitimacy of the XML document.

2. Use CSS to display XML documents

1. What is CSS?

CSS stands for Cascading Style Sheets.
· Styles define how HTML elements are displayed
· Styles are usually stored in style sheets
· External style sheets can greatly improve work efficiency and are usually stored in CSS files
· Multiple style definitions can be cascaded into one

2. What are the advantages of using CSS to display XML data?

·Rich expression effects

·Good readability

·Facilitates information retrieval

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" ?>
<persons xmlns:HTMIL="http://www.W3.otE/Profiles/XHTML-transitional">
   <HTML:style>
       person{
           display:block;
           font-size:25pt3
           color:red;
       }
   </HTML:style>
       <person>
           <name>Zhang San</name>
           <sex>male</sex>
           <age>25</age>
       </person>
       <person>
           <name>Li Na</name>
           <sex>female</sex>
           <age>24</age>
       </person>
</persons>

Small document size

3. Summary of commonly used CSS

4. CSS definition style

CSS rule sets are composed of selectors and declaration blocks. The basic format is as follows:

selector
{
Attribute 1: attribute value 1; /*statement*/
Attribute 2: attribute value 2;
.....
Attribute n: attribute value n;
}

5. Combination of XML and CSS

5.1 Calling external style sheet files

<?xml-stylesheet type="text/css" href="URL of CSS file"?>

5.2 Define CSS styles inside XML documents

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" ?>
<persons xmlns:HTMIL="http://www.W3.otE/Profiles/XHTML-transitional">
      <HTML:style>
            person{
                display:block;
                font-size:25pt3
                color:red;
           }
      <HTML:style>
           <person>
                <name>Zhang San</name>
                <sex>male</sex>
                <age>25</age>
                </person>
           <person>
                <name>Li Na</name>
                <sex>female</sex>
                <age>24</age>
            </person>
</persons>

6. CSS display rules

When you open an XML file with a browser, the browser will display the content contained in the element according to the “order” in which the elements appear in the XML file and use the corresponding style rules in the element’s CSS. If an element does not have a corresponding style rule in CSS, the browser will use the default rules to display the content contained in the element.
Normally, the display format attribute set for an element in CSS will affect all sub-elements contained in the element, unless these sub-elements have different format attributes set again. Therefore, if no specific style rules are set for a child element, the child element will automatically inherit the rules of the parent element.
If there are no style rules set for an element in the style sheet, and there are no style rules for the parent element to inherit, the browser
The browser will use its own default rules for display.