Getting Started with XML (bottom)

Source: Internet
Author: User
Tags add format define object definition header object model valid
Xml


3. Well-Formed XML document vs. valid XML document

A properly written XML document can have two forms: it can be well-formed or valid. If an XML document is written to conform to the rules listed in the XML standard, Then it is considered to be in the right format. If an XML document is written with a standard file data format or organizational structure, it is considered valid.


When using XML to exchange data, effectiveness is very important. If I have an XML document about invoices and I want to exchange data with my business partners, what should I do? Obviously, I first want to tell my partner the file format I want and the format my document will conform to.



There are currently two ways to define the format of an XML document in detail: Document Type Definition (DTD) and XML SCHEMA.DTD are part of the XML1.0 standard, So they are currently the most popular method of definition. But the problem is that the syntax used by DTDs is very intuitive, which runs counter to the purpose of XML. And DTDs don't allow you to specify the types of different elements.



The XML Schemas specification is proposed by Microsoft and other XML industry companies and is considered a replacement for future DTDs by the Consortium. XML Schemas uses a class XML syntax to describe an XML document. In addition, XML schemas provides the ability to describe element types in a file ( Through Xml-data). In our view, for those non-SGML professionals, schemas is easier to read, easier to understand, and easier to write than a DTD.



XML allows you to easily define your own file formats and tags, but if everyone uses his own defined file format, no one will be able to exchange similar file information with each other. There are now two more formal "common document" standards, and the two are antagonistic. In this confrontation, Many companies have to painfully drift between the two camps.



BizTalk. The Microsoft-led organization wants to build a platform for general business documents (invoices, orders, etc.).

Xml.org. The organization stands in opposition to Microsoft, advocating the use of DTDs to define basic business file formats.



It is too early to comment on which criteria will win the game. The most likely scenario in the future is two to three popular file formats, and the conversion tool helps people convert files from one format to another.



4.XML Document Object Model



If you already have an XML file, how do you work with it? In the next section, we'll show you how to convert an XML file to an HTML file and display it in a browser, and this section is about using XML document Object Model (DOM) To read and write XML files. When an XML processor parses an XML document, the processor saves the document to a memory space tree. DOM is a programmatic interface to access the tree through which you can read, add, delete, and edit nodes in the tree.

Figure A shows the situation where our instance file is saved to a memory space tree. Each DOM tree starts with a file object and all the data is saved.





Figure A

In IE5, the XML DOM can be accessed through any program or scripting language. For example, JavaScript in table B will change the route of the first American Airlines flight:



Table B: Use JavaScript to change the route of American Airlines ' first flight



var myDocument = new ActiveXObject ("Microsoft.XMLDOM");

Mydocument.load ("Flights.xml");

MyDocument.documentElement.childNodes.item (0). Childnodes.item (3). Text

= "American Airlines";



The first line of the program creates an empty DOM object. The second line introduces our data files to the DOM object by using the Document.load () method. Finally, on the last line, use a long command to access the nodes of the first flight's route and change the "American Airlines". The contents of the node. All this is done through the ChildNodes property, which allows us to access a child node through a numeric index. Dom also provides a number of other ways that we can access nodes through names or other properties.

The example above is obviously very general, but it's a good way to show us what a program about DOM looks like. If you want to do some work with XML, you have to understand the DOM and learn how to manipulate and access the Document object tree.



5. Display XML



When displaying XML data, you have two choices: Create a cascading style Sheet (CSS) to produce HTML based on that data, or use another to immediately become a standard XML technology, extensible Format language Extensible style Language.

Choosing which method depends on the complexity of displaying the data. CSS is a good choice if you only want to have a basic output. For example, you can generate a simple form, flight information with gray, airline information with blue color, font with Helvetica. For more complex output, You need to use XSL. If you want the user to click on a title and get a list of flights, and you want to represent your data in a variety of ways, so XSL is a better choice for you. Similarly, XSL uses a class XML syntax to describe how to convert XML to HTML. So for you it may not be as unfamiliar as CSS and difficult to understand.

Now let's take a look at our flight example, we use XSL to convert it to a format that can be displayed--table C shows how we do that.



Table C: Use XSL to convert it to a format that can be displayed:

<?xml version= ' 1.0 '?>

<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >

<xsl:template match= "/" >

<HTML>

<BODY>

<table border= "2" >

<TR>

<TD>Destination</TD>

<TD>Airline</TD>

<TD>Movie</TD>

<TD>Meal</TD>

</TR>

<xsl:for-each select= "flight_schedule/flight" order-by= "Destination" >

<TR>

<td><xsl:value-of select= "Destination"/></td>

<td><xsl:value-of select= "Airline"/></td>

<td><xsl:value-of select= "movie"/></td>

<td><xsl:value-of select= "Meal"/></td>

</TR>

</xsl:for-each>

</TABLE>

</BODY>

</HTML>

</xsl:template>

</xsl:stylesheet>



The first line is the standard header for XSL (you can learn more about its exact meaning through the XSL work document published by the consortium).

<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >

The next line tells the XSL processor to process each record in the XML file.

<xsl:template match= "/" >

The XSL file can contain HTML tags. They are HTML elements that define the beginning of an HTML form. We then have an XSL For-each declaration stating that we will cycle through each flight element in the order of destination.

<xsl:for-each select= "flight_schedule/flight" order-by= "Destination" >

In each loop, the HTML tag makes up the elements of the form with the value of the XSL selection declaration, extracts the corresponding values from the flight elements to be obtained, and converts them into strings to be placed into the HTML form:

<TR>

<td><xsl:value-of select= "Destination"/></td>

<td><xsl:value-of select= "Airline"/></td>

<td><xsl:value-of select= "movie"/></td>

<td><xsl:value-of select= "Meal"/></td>

</TR>

Figure B shows the results of these XSL commands applied to airline flight data files.



Figure B

Note: If you add the following processing instructions to the XML header, IE5 automatically applies the XSL command to the specified XML file.

<?xml-stylesheet type= "text/xsl" href= "flight1.xsl"?>

XSL is a very advanced language. If you want to learn more details, you can read the relevant standards on www.w3.org or go to the Msdn.microsoft.com/xml to read Microsoft's excellent online User guide.



Use XSL to achieve a variety of representations



An important application of xml/xsl is to achieve a variety of manifestations. Because data and representations are separate, unlike in HTML where you need to write a new form of presentation and have to reintroduce all the data, you can simply change the XSL to produce a new representation. For example, If a user wants to classify flight data by type of aircraft, you can use the following code to provide an "aircraft classification display"

<xsl:for-each select= "Flight_schedule/flight" order-by= "aircraft" >

When displayed, an aircraft classification table, as shown in Figure C, is produced.



Figure C



How about 6.Netscape?



Most of the examples here are focused on the Internet Explore 5.0 support for XML, so what is Netscape Navigator's support for XML? You may have heard the next Netscape browser, what people now call Gecko will be based on an open source programme called Mozilla.org. For gecko support for XML, detailed instructions are already available on the mozilla.org site. Here we show the reader a few notes about it.

In Gecko, mozilla.org will use a free XML parser called expat.

will provide full support for the XML/CSS.

There is no mention of XSL because it is not yet a standard.

DOM is fully supported.

The Gecko user interface will use an xml-based language xul,xml-based user-interface language shorthand. If you want to know more details, see www.mozilla.org/rdf/doc/xml.html.

Although the current IE5 market share is increasing, it is likely that you will still have to wait until an XML-enabled Netscape browser is released to truly support XML on your browser. But that doesn't prevent you from using XML on the server side today.



Server support for XML



We tested Microsoft's Internet Information Server 5.0 and found that it supports all the XML features we've tested. You can use Internet server api,active server Pages, or CGI scripting language to introduce XML, using the same XML parser as IE5.



When using XML on the server side, you want to load an XML file into memory, or use active Data Objects (ADO) to generate an in-memory image. Once you've loaded an XML file on the server side, you can manipulate it through the DOM and swap it with a business partner. , or convert it to an HTML file using XSL or CSS to display to the user's browser.

Another feature is that you can detect what type of browser the user is using. If the user's browser is IE5, you can send the XML data to the browser end and then operate on the client. If it's another browser, You can do a purely server-side operation. This strategy will prepare you for the future of XML in vogue.



7. Summary



Here's a summary of the advantages of using XML to build Web applications:



XML allows you to easily exchange data with business partners.

XML allows you to freely introduce and manipulate data on your server or client side. This allows you to easily combine data from different sources on your server.

With XML on your browser, you can avoid cumbersome data manipulation. Imagine if our flight data is 2MB, we have to resend each time the user wants to change the display. If XML is used, the data is saved in the browser, We can change the representation of these data very easily.

With XSL, you simply change the description of the display form, You can implement a variety of representations for any one XML data. You don't have to reintroduce that data on every page. and updates to the data will automatically be reflected in the way your data behaves. If you've ever worked on object-oriented programming or studied model design, You will find that this is the same as the universally accepted model-view-controller pattern.



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.