XML Technology Guide (2)

Source: Internet
Author: User
Tags format define object definition include object model access
Xml

Schema

A schema is usually a set of rules that are predetermined in order to describe a class of given XML documents. It defines the individual elements that can appear in the specified XML document and some attributes related to an element. It also defines structured information about XML documents, such as which elements are child elements of other elements, the order in which child elements appear, and their number. It can also define whether an element is empty, whether it contains text, or whether a property has a default value.

DTDs (document TYPE definitions) and XML data are concrete examples of how XML document plans are described.

Document type definition (DOC type definitions)

The DTD language is developed specifically to define the validation rules for SGML documents. Because XML is a subset of SGML, DTDs can also be used to define the determination rules for XML. Unlike XmlSchema, an XML processor can use a DTD at run time to determine the legality of an XML.

The syntax of a DTD may sometimes be obscure. DTDs uses different syntactic elements, such as exclamation marks, parentheses, asterisks, angle brackets, and so on, to define which elements are required in an XML document, what is optional, and the number of elements that can occur, and so on. DTDs also defines relationships between elements and attributes to different elements.

The following is the Hamburger.xml DTD (HAMBURGER.DTD) listed earlier:

<! ELEMENT Hamburgers (Hamburger) *>
<! ELEMENT Hamburger (name, description, Price) >
<! Attlist Hamburger Lowfat CDATA #IMPLIED >
<! ELEMENT name (#PCDATA) >
<! ELEMENT description (#PCDATA) >
<! ELEMENT Price (#PCDATA) >

This document states that the hamburgers element can contain multiple hamburger elements. At the same time, each hamburger element must contain a lowfat attribute and three child elements, all of which are #pcdata (parsed character data). Documents that follow this DTD must include the following line of code:

<! DOCTYPE Hamburgers SYSTEM "HAMBURGER.DTD" >

This statement tells the parser that the contents of the XML document are considered legitimate regardless of the schema in the DTD

Although MSXML 2.0 supports DTDs, you'll find it hard to use them. It is very complex and difficult to master and use. Note that DTD syntax is not a legitimate XML. Because of this, XML processors also support the DTD syntax used to describe schemas in addition to XML syntax. Imagine that if we were to describe schemas in XML, it would be much easier for developers, especially the providers of XML tools, to work on the validation of XML documents. The consortium is considering several schemes to make up for DTDs deficiencies to improve the current syntax definition process.

XML data

Xml-data is a XmlSchema language. In Microsoft's definition, Xml-dataschema usually refers to XmlSchema, not dtdschema. A xml-dataschema is an XML document with a good structure. The Xml-data language is based on the Xml-data DTD, which indicates the desired schema definition format. Because Xml-dataschema is a simple XML document, any tool used in XML documents can be used to define Xml-dataschema.

The following Xml-dataschema generated schema is the same as the schema previously defined by HAMBURGER.DTD:

<?xml version= "1.0"?>
<schema xmlns= "Schemas-microsoft-com:xml-data" >
<elementtype name= "Name"/>
<elementtype name= "description"/>
<elementtype name= "Price"/>
<attributetype name= "Lowfat"/>
<elementtype name= "Hamburger"/>
<element type= "Name" maxoccurs= "1"/>
<element type= "description" maxoccurs= "1"/>
<element type= "Price" maxoccurs= "1"/>
<attribute type= "Lowfat" maxoccurs= "1"/>
</ElementType>
<elementtype name= "Hamburgers" model= "closed" >
<element type= "Hamburger" maxoccurs= "*"/>
</ElementType>
</Schema>


When defining elements and attributes in Xml-dataschema, the <ElementType> and <AttributeType> elements are used respectively. They provide definitions of elements and attribute types. Use <element> or <attribute> tags when defining an element or attribute. You can specify the number of elements that an element is allowed to appear by defining minoccurs/maxoccurs. The schemaxml structure also defines where elements are allowed to appear in an XML document (for example, a
Microsoft provides support for Xml-data through MSXML 2.0. According to Microsoft's XML SDK documentation, the implementation of the XmlSchema bundled in IE 5 is largely based on the Xml-data note issued by the consortium in January 1998. It provides support for a subset of Xml-data, which, although slightly different from the syntax of XML, coincides directly with the functionality stated in DCD.

Processor (API) technology

As we've mentioned earlier, in order to use XML effectively, you have to programmatically access the data. We refer to a software module that can access an XML document while providing access to its content and data structure, called an XML processor or an XML API.

While developers are completely free to develop or use their own XML APIs, I recommend them to use industry-standard APIs in their interests. Because only the industry-standard API is accepted, the code written by the developer can be implemented smoothly in other environments without modification.

At present, there are two main APIs have been widely used by the vast number of developers, is about to become the future industry standards. They are: DOM (Document Object Model) and sax (simple APIs for XML).

DOM Document Object Model

The Document Object model is a standard for programmatically accessing data and structures in an XML document. The consortium has agreed to include it as a recommended target for the first level of future industry standards.

The DOM is a tree-like structure based on XML documents in memory. When an XML file is loaded into the processor, a corresponding tree is created in memory (see Figure 1). The DOM also defines a programming interface for traversing an XML tree and managing individual elements, values, and attributes (including the names of methods and properties).

-->



Figure 1. XML in-memory Representation

MSXML 2.0 fully supports the DOM and provides an Easy-to-use object model to interact with in-memory trees. Here is a simple example of VB that shows how to use MSXML to traverse all the child elements of a tree.

Set xmldoc = CreateObject ("MSXML". DOMDocument ")
bsuccess = Xmldoc.load ("Hamburger.xml")
If bsuccess Then
For each node in XmlDoc.documentElement.childNodes
val = Node.text
Next
End If

Sax

One of the main drawbacks of the DOM standard is the huge overhead associated with loading an entire XML document into memory. When the amount of data in a file is very large, this can cause some problems for us. When you transmit such a large XML file on the intranet or on the Internet, you may not be able to wait for all the file transfers to begin processing data. Many XML developers are aware of this, so they work together (starting with the Xml-dev mailing list) to start creating another new standard. This is sax. Although Sax is still in the early stages of development, because of its excellent performance, it is fast to get everyone's welcome.

Sax is a very simple XML API (just as its name is, simply API for XML) that allows developers to use event-driven XML parsing. Unlike the DOM, Sax does not require that the entire XML file be loaded into memory. Its idea is very simple, once the XML processor completes the operation of the XML element, it immediately calls a custom event handler to process the element and related data in a timely manner. Although this can greatly improve the efficiency, but also will create a certain problem. For example, developers will have to be limited in flexibility. If you want to know more detailed information, please visit http://www.megginson.com/SAX/sax.html.

Conversion technology

Once you start using the standard DOM APIs to interact with XML data, you'll find it tedious to get a specific piece of data from a large document, or to convert one part of an XML document into another format, such as HTML.



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.