Verify XLM Data Validity (favorites)

Source: Internet
Author: User
Verify XLM Data Validity (favorites)

1. xml validity Verification

one of the main objectives of XML technology is to facilitate information exchange. Obviously, this goal can be achieved only when the format or structure of the XML document is unanimously recognized by all parties involved in the interaction. XML schema and DTD are the models used to describe the information structure. They can be used to verify the validity of XML documents.
the schema and DTD define the elements and attributes that can be used in the document, and the possible combination of these objects and text content. As a standard for document verification, the schema and DTD should not only be understood, but also be easy to understand for the application Program . There are two ways to verify the validity of a document: one is to verify the syntax structure, that is, validity of the object itself and its structure, such as the element type, element nesting format, attribute type, attribute value data type, and whether the attribute value is optional. On the other hand, it is the semantic verification, only by truly understanding the meaning of an XML document can information interaction be realized. Otherwise, only information with correct structure but fuzzy meaning is useless to the recipient, sometimes there may be misunderstandings, which play the opposite role.
perform the following steps to verify the XML document:
1. define an XML schema or DTD: schema or DTD defines structural standards for all documents of the same type. For example, documents related to book information can share a standard.
2. Application Schema or DTD: Generally the schema is specified by the author of the document, so that the recipient can quickly verify the document according to the instructions. However, in the C/S (Client/Server) environment, the server cannot determine whether XML has been verified on the client. Therefore, to ensure the verification process and avoid possible repeated verification, the server performs schema or DTD application operations on the document.
3. Run the parser for verification: the parser scans the structure while loading the document, terminates the loading process if any error occurs, and responds appropriately.

2. Differences between Schema and DTD
Although both XML schema and DTD are used for document verification, there are some differences between them. The former is more accurate and flexible:
● XML schema is an open model with high scalability and functionality. DTD is a closed model with poor scalability;
● XML Schema supports a wide range of data types to fully meet the needs of network applications, especially e-commerce. DTD does not support element data types, and attribute types are limited;
● XML Schema supports the namespace mechanism, while DTD does not;
● XML schema can be used to verify the entire XML document or document in different regions, but DTD lacks the flexibility;
● The syntax of XML Schema fully complies with XML specifications and can be used in combination with DOM technology. It has powerful functions, while the DTD syntax is self-contained and difficult to learn.
Therefore, schema is likely to replace DTD as a new Standard for verifying the legitimacy of XML documents.
 
3. Basic Elements of Schema
Schema has eight elements: <schema>, <elementtype>, <element>, <group>, <attributetype>, <attribute>, <datatype>, and <description>. These elements define the syntax and structure allowed in XML. Note: These elements must be defined before schema is used.

1. <schema> element
The schema file is very similar to other XML documents. The root element is <schema>, indicating the document type:
<? XML version = "1.0"?>
<Schema name = "myschema"
Xmlns = "urn: Schemas-Microsoft-com: XML-Data"
Xmlns: dt = "urn: Schemas-Microsoft-com: datatypes">
</Schema>
The attribute name specifies the schema name and xmlns specifies the namespace. The first xmlns specifies the default namespace for the full text, and the second defines the namespace of the data types available in the document.
To reference schema in XML, specify the URL address of the schema file by using the namespace in the root element of the XML file. prefix "X-schema" before the URL:
<? XML version = "1.0"?>
<Myelement xmlns = "X-schema: http://mysite.com/myschema.xml">
</Myelement>
If you use xmlns to reference schema in an element of an XML document, it is to verify the local legitimacy of the document.

2. <elementtype> element
<Elementtype> elements are used to declare new elements used in XML documents:
<Elementtype
Name = "element tag name"
Content = "empty" | "textonly" | "eltonly" | "mixed"
Model = "open" | "closed"
Order = "one" | "seq" | "sequence"
DT: TYPE = "XML data type">
</Elementtype>
The content attribute specifies whether the declared element is null, whether it contains text or child elements, or both. Order indicates the arrangement order of the child elements; model specifies whether an element can contain elements or attributes not defined in the schema. DT: Type specifies the Data Type of the element.

3. <element> element
The <element> element defines the content of the elements declared by <elementtype>, indicating which child elements can be used in the specified Element type:
<Element
Type = "declared element type"
Minoccurs = "0" | "1"
Maxoccurs = "1" | "*"/>
The Type attribute is necessary and must be consistent with the name attribute in <elementtype>. The minoccurs and maxoccurs attributes specify the minimum and maximum number of occurrences of an element. The default value is 1. If minoccurs is 0, the description element is optional. If maxoccurs is *, the number of times that the description element appears is not limited.

4. <group> element
<Group> elements also appear in <elementtype> to express the concept of element grouping. Objects in a group can be elements or child groups. The Order attribute specifies the order of elements in the group:
<Elementtype name = "...">
<Group minoccurs = "0" | "1"
Maxoccurs = "1" | "*"
Order = "one" | "seq" | "sequence">
<Element type = "…" />
</Group>
</Elementtype>

5. <attributetype> element
The <attributetype> element defines the attribute types used in the schema. The function range varies depending on the location where the image appears. If it is defined in the <elementtype> element, the defined attribute type can only be applied to the element type. If it is defined outside, this attribute can be used for all element types in the document.
<Attributetype
Name = "attribute name"
DT: TYPE = "XML data type"
Required = "yes" | "no"
DT: Values = "enumerated list of values"
Default = "Default Value">
</Attributetype>
Name is the name of the property type. DT: type is the same as the usage in the <elementtype> element. Required specifies whether this property is necessary for the element that references it. DT: values only applies when DT: this parameter is valid only when type is "enumeration". In this case, it lists all possible values of the attribute. Default specifies the default value of the attribute type and must be a valid value. For example, if the attribute type is Enumeration type, the default value must be one of the values listed in DT: values.
6. <attribute> Elements
The <attribute> element defines the attributes declared by <attributetype>:
<Attribute
Type = "declared attribute type"
Required = "yes" | "no"
Default = "Default Value"/>
The Type attribute uniquely specifies the referenced property type. The value must be exactly the same as the name attribute in <attributetype>. Required indicates whether the attribute is necessary for the element that references it; default specifies the default value of the property type. If the default and required attributes corresponding to <attributetype> and <attribute> of the same attribute are defined, the priority level of <attribute> is higher, and the value of this attribute prevails.
The other two elements <datatype> and <description> define the data type of the schema element and provide instructions for the element. The usage is simple and will not be described in detail in this article.

1. Create a schema
Consider the following XML document books. xml:
<? XML version = "1.0"?>
<Booklist>
<Book>
<Title> straight talk about computers </title>
<Author> Lars Peterson </author>
</Book>
<Book>
<Title> You Can comabt computer stress </title>
<Author> Lars Peterson </author>
<Author> Carlos Diaz </author>
</Book>
</Booklist>
When creating a schema for the preceding XML document, you can first set rules for <title> and <author> elements and define them as text elements that can only contain strings with <elementtype>:
<Elementtype name = "title" content = "textonly" DT: TYPE = "string"/>
<Elementtype name = "author" content = "textonly" DT: TYPE = "string"/>
The <book> can only contain child elements, and the sequence in which child elements appear is certain. The <element> definition <book> is used internally to include a <title> and any <author> sub-element:
<Elementtype name = "book" content = "eltonly" Order = "seq">
<Element type = "title"/>
<Element type = "author" maxoccurs = "*"/>
</Elementtype>
Finally, use the same method to describe the root element <booklist>:
<Elementtype name = "Booklist" content = "eltonly">
<Element type = "book" minoccurs = "0" maxoccurs = "*"/>
</Elementtype>
The following example defines an attribute of the "ISBN" type: the data type is a string and requires that if the attribute is referenced in an element, a value must be assigned.
<Attributetype name = "ISBN"
DT: TYPE = "string"
Required = "yes"/>
<Elementtype name = "book"
Content = "eltonly">
<Attribute type = "ISBN"/>
</Elementtype>

2. xml scalability
Because XML schema is an open content model, this scalability means that users can use their own defined elements and attributes in XML schema. For example:
<Elementtype name = "price"
Xmlns: myext = "urn: myextensions" DT: TYPE = "float">
<Myext: salestax> 17.5 </myext: salestax>
<Myext: bulkbuy> 20 </myext: bulkbuy>
<Myext: discount> 5.0 </myext: discount>
</Elementtype>
The local attribute definition only declares the <price> element as the "float" type, the other three tags are referenced in the namespace to describe the sales tax, batch quota, and discount rates of books respectively. This scalability enhances schema flexibility.
We can also use DOM technology to access the extended Schema:
<Book ISBN = "9-001-122-01">
<Title> straight talk about computers </title>
<Price> 19.99 </price>
</Book>
Suppose that the above XML data is defined according to the schema just now, and we have used DOM to locate the <book> element, then we can access the <price> node, use its definition attribute to get the definition of <elementtype> from Schema:
Set pricenode = booknode. selectsinglenode ("price ")
Set priceelementtypenode = pricenode. Definition
Salestax = priceelementtypenode. childnodes (0). Text
Bulkbuy = priceelementtypenode. childnodes (1). Text
Discount = priceelementtypenode. childnodes (2). Text
In this way, you can process the data as needed.

3. Verify XML in the Client/Server Environment
In the C/S (Client/Server) environment, both the client and server can use schema to verify the document. The object verified on the client can be a document sent from the server or a document to be sent. The following example uses a schema named mermerschema. XML to verify the docsubmit XML document and then sends it to the server:
Set rootnode = docsubmit.doc umentelement
Rootnode. setattribute "xmlns ",
"X-schema: http: // server/customerschema. xml"
Dom first obtains access to the root element, and then sets xmlns as the schema address to start verification.
Compared with the client, it is more important to verify on the server and more common in practice. Since the number of customers connecting to the server is very large, it is necessary to check whether the received documents are consistent with the expected structure before processing the data they send. The verification procedure is as follows:
1. Load the XML document into the DOM tree and start Verification:
Set docreceived = Createobject ("Microsoft. xmldom ")
Docreceived. validateonparse = true
Docreceived. async = false
Docreceived. Load request
2. Determine whether to link the XML schema (you only need to check whether the xmlns attribute is set in the root element ):
Set rootnode = docreceived.doc umentelement
If rootnode. getattribute ("xmlns") =
"X-schema: http: // sever/customerschema. xml"
Then
<〈! -- Yes. Start verification! --> --〉
Else
<〈! -- No ...... --> --〉
End if
3. If there is no link, the server needs to specify a schema for the link, and then reload the updated document to another DOM tree:
Path = server. mappath ("mermerschema. xml ")
ATTR = "X-schema:" & Path
Rootnode. setattribute "xmlns", ATTR
Set doctested = Createobject ("Microsoft. xmldom ")
Doctested. validateonparse = true
Doctested. async = false
Doctested. loadxml docreceived. xml
Because XML schema has many outstanding features, many famous international companies and large enterprises are beginning to tilt towards schema. For example, Microsoft's IE 5.0 already supports XML schema. Although the schema still needs to be gradually improved in practice, replacing the DTD with XML schema is the trend of the times. Therefore, I suggest learning and mastering this technology.

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.