Home >Backend Development >XML/RSS Tutorial >XML Development Basics-XML Validation Code Sharing

XML Development Basics-XML Validation Code Sharing

黄舟
黄舟Original
2017-03-25 17:11:131579browse

XMLwith correct syntax is called "well-formed" XML.

XML that has been verified by a certain DTD is "valid" XML.

Well-formed XML document

A "well-formed" XML document has correct syntax.

A "well-formed" XML document will comply with the XML syntax rules introduced in the previous chapters:

The XML document must have a root element

The XML document must have a closing tag

XML tags are case-sensitive

XML elements must be nested correctly

XMLAttributesMust be quoted

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>

Validating XML documents

A valid XML document is a "well-formed" XML document that also adheres to the syntax rules of the Document Type Definition (DTD):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>

In the above example, the DOCTYPE declaration is A reference to an external DTD file. The following paragraphs show the contents of this file.

XML DTD

The function of DTD is to define the structure of XML document. It uses a series of legal elements to define the document structure:

<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to   (#PCDATA)>
<!ELEMENT from  (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body  (#PCDATA)>
]>

XML Schema

W3C Supports an XML-based DTD replacement called XMLSchema:

<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to"   type="xs:string"/>
<xs:element name="from"  type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body"  type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

The above is the detailed content of XML Development Basics-XML Validation Code Sharing. 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