"Javaweb learning" XML and Constraint patterns

Source: Internet
Author: User
Tags cdata file url


XML: Extensible Markup Language, published by the organization, for storing relational data as a configuration file, describing the relationship between program modules

<?  ?>

encoding Specifies the character encoding of the document, standalone The document is independent, and if Yes indicates that the XML file does not require a DTD file to verify that the token is valid or that the XSL, CSS Control appearance display

elements: defined by a tag, including the start and end tags and their contents, < book > in- Depth Experience Java Web Development Insider </ Book > (tags can be nested; well-formed XML documents have only one root tag, other tags are descendants of the root tag; empty lines and spaces in XML are parsed as raw content)

Naming conventions: case-sensitive,<a></a> and <a></a> are two different labels; cannot start with a number, underscore, or XML; cannot contain spaces, colons:

Properties: XML parsing properties are faster than parsing sub-tags, attribute values must be caused by double or single quotes, and attributes can be stored as sub-labels

<name= "txt1"/><input>     <name>txt1</name></  Input>

CDATA Extents: the contents of which are not parsed by the XML parsing engine, but are displayed as raw content, <![ cdata[ This is the content of the CDATA zone ]]>

processing instructions: called PI, used to direct the XML parsing engine how to parse XML to "<?" At the beginning, End with "?>", for example: document declaration <? XML version= "1.0" ?> ; <? xml-stylesheet type= "text/css" href= "1.css " ?> , which informs the XML parsing engine to control the appearance of the XML display using CSS files


XML Constraint pattern: defines the markup and structure of an XML document, similar to a data table structure. The content of the XML constraint pattern also follows certain grammatical rules, among which there are 2 main types:XML DTD and XML Schema

A DTD constraint can be written as a separate file (with a. DTD suffix) or in an XML file. This includes the definition of relationships between elements, the definition of element attributes, and the definition of entities and symbols.

Case 1:BOOKSHELF.DTD

<! ELEMENT Bookshelf (book +)><! ELEMENT Book (name, author, price)><! ELEMENT name (#PCDATA)><! ELEMENT author (#PCDATA)><! ELEMENT Price (#PCDATA)>

In the Book.xml

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Bookshelf SYSTEM "BOOKSHELF.DTD"><Bookshelf>    <Book>        <name>Experience the insider of Java Web Development in depth</name>        <author>Zhang Xiaoxiang</author>        <Price>59 USD</Price>    </Book></Bookshelf>

The following is displayed in the Chrome browser

Attention:
1. XML can have only one root node
2. There is a space between the "book" and the Brackets "()", and the "Name" and "(#PCDATA)" are also
3. If there is no "bookshelf", the "book" as a node, will also error

An XML document references an external DTD constraint

Referencing local DTD files <! DOCTYPE Document root node SYSTEM "DTD file URL" >

Referencing common DTD files <! DOCTYPE Document root node Public "DTD name" "DTD file URL" >

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >


Case 2: Writing DTD constraints in an XML file

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Bookcase [<! ELEMENT Bookshelf (book +)>    <!ELEMENT Book (name, author, price)>    <!ELEMENT name (#PCDATA)>    <!ELEMENT author (#PCDATA)>    <!ELEMENT Price (#PCDATA)>]><Bookshelf>    <Book>        <name>Experience the insider of Java Web Development in depth</name>        <author>Zhang Xiaoxiang</author>        <Price>59 USD</Price>    </Book></Bookshelf>

DTD constraint mode

the format for declaring XML elements in a DTD file : <! Element element type > , element type can be element content or type, if the element content, to be wrapped up in parentheses, if it is type, write directly

<! ELEMENT Book (name, author, price) >
<! ELEMENT Book (title | author | price) >
<! ELEMENT name (#PCDATA) >
<! ELEMENT Book empty>
<! ELEMENT Book any>

element content with a plus "+" means that the element appears >=1 times, "?" Represents 1 or 0 occurrences, and "*" indicates how many times it will be done.

    • Line 1th, the element "book" has three sub-elements "name" "Author" "Price", and the order must be consistent with the declaration
    • Line 2nd, the element "book" can only have "name" "Author" "Price" one of the three as a child element
    • Line 3rd, the element type is #PCDATA
    • Line 4th, element type is empty, such as <br/>
    • Line 5th, Element type arbitrary


The format of the element attribute definition in the DTD file (the element must already be declared before the attribute can be declared):

<! Attlist element Name Property Name 1 property value type set Description Property Name 2 property value type set description ...

The attribute type has CDATA , enumerated , ID , the first representing the property value as a normal text string, and the second representing that the property value can only be one of a set of property values, such as <! attlist person Sex (NAN|NV) #IMPLIED > , Third represents the ID of the element, the value is unique

The setting description has #REQUIRED The property must be set, #IMPLIED Optional, #FIXED The property value is fixed to a value, and no other value can be set for the property in the XML file. However, you need to provide this value for this property, using the default value directly: You can set the value in the XML or not set the property value. If not set, the default value is used.

<! attlist     Person #REQUIRED    #REQUIRED    (NAN|NV) #IMPLIED nan    # implied>

Schema constraint mode

Schema is better than DTD, has become the standard of the organization, is gradually replacing the DTD

Namespaces : use namespaces to differentiate each constrained schema document, with each namespace represented by a unique URI.

namespace declaration: is to specify a temporary abbreviation for the namespace of a constrained schema document in XML, which will be the prefix name of the element and attribute. A namespace declaration is very similar to an element's property definition, can be in the opening tag of any one element, and multiple namespaces can be declared in one element, and the basic format of the namespace declaration is xmlns: prefix name = "URI" , The prefix name is a temporary abbreviation. (xmlns is shorthand for XML namespace). The default namespace xmlns= "URI" , that is, the prefix name is omitted.

<xs: Schema xmlns: xs= "Http://www.w3.org/2001/XMLSchema" >

Using namespaces to introduce XML Schema documents

The above tag only knows that "Http://www.w3.org/2001/XMLSchema" is a URI that represents a namespace and does not know the access address of the namespace XML Schema document, so the XML document cannot be validated.

Use xs: schemalocation To specify the access address for the namespace, using the namespace address , separated by a space. xmlns: xs= "Http://www.w3.org/2001/XMLSchema" This namespace is well known, all without having to specify its access address.

< bookshelf xmlns= "Http://www.xxx.com/bookshelfSchema" xmlns: Test= "Http://www.demo.com/testSchema" xmlns: xs= "Http://www.w3.org/2001/XMLSchema"         xs: schemalocationhttp://www.xxx.com/ Bookshelfschema http://www.xxx.com/xsd/bookshelf.xsd                             http://www.demo.com/testSchema/        http Www.xxx.com/xsd/test.xsd                                  "
>









"Javaweb learning" XML and Constraint patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.