Chapter 3 First XML Document
This chapter teaches readers to create simple XML documents with markers that they define themselves. The reader will learn how to write a style order for use in how to describe the contents of the tag content in the document. Finally, learn how to put documents into a web browser to view.
Since this chapter uses example to explain, not from the principle, it will not involve many details. Experienced readers will notice that several exceptions and special circumstances are not discussed in this chapter. Don't worry about it. It will be discussed in the next few chapters. For most content, there is no need to care too much about technical content. As HTML is the same, you can also learn from the simple example created by others and modify it according to your needs.
In order to achieve the above object, I encourage you to step by pressing the samples I have given in this chapter and putting these code into different programs discussed. This will cause the reader to generate basic feelings for XML, which will make it easy in the technical details mentioned in the next few chapters.
The main contents of this chapter include:
· ???????? Creating a simple XML document
Carefully study this simple XML document
Dedicated to XML tags to concrete
Additional style list on XML documentation
3.1 Hello XML
This section follows the tradition of the new language in this section, first introduces the program that can print "Hello World" on the screen. XML is a tag language, not a programming language, but the basic principle is still applicable. The easiest way is to start with a completely running example, instead of trying to start with a more basic without any function. If the user does have problems when using basic tools, in a short document environment, it is more easily debugged and corrected in a complex document environment.
In this section, the reader will learn how to create a simple XML document and save it in the file. Then we carefully examine the code and its meaning.
3.1.1
Create a simple XML document
In this section, the reader will learn how to type an actual XML document. We start with the simplest XML documentation that can be imagined. This document is listed in Listing 3-1:
Listing 3-1: Hello XML
Hello XML!
This is not very complicated, but it is a "good" XML document. More precisely, this is a complete XML document (there are some special terms for documentation in XML, which is considered "good" in accordance with which rule is satisfied. Among them, "structural complete" is a such thing. The term is discussed later in this book.) Type this document in any convenient text editor such as NotePad, BbEdit, or Emacs.
Structural integrity will be discussed in Chapter 6, "Structure Complete XML Document".
3.1.2
Save an XML file
When you type the above code, save the document in a file called Hello.xml. You can also use the names such as HelloWorld.xml, MyFirstDocument.xml, but other file names, but the extension of the three letters is standard, generally do not change. Moreover, be sure to save in a normal text format, not to use some word processing programs such as the built-in format of WordPerfect or Microsoft Word.
If you are using NotePad on Windows 95/98 to edit files, when saving documents, you must enclose the file name with double quotes, ie "Hello.xml", not just hello.xml, just as shown in Figure 3-1 The same as shown. If there is no quotation, NotePad will add the .txt extension after the file name, that is, the file name turns hello.xml.txt, which is not what we want.
Figure 3-1 Saving an XML document with a file name with quotation marks in NOTEPAD
The Windows NT version of NOTEPAD will also give options that save files as Unicode formats. Surprisingly, this can be preserved, but we still insist that the basic ASCII text format is better. The XML file can be either a Unicode format or a compressed version of UTF-8 in Unicode, which is a strict ASCII's superchard, so the pure ASCII file is also a legal XML file. UTF-8 and ASCII will be discussed in more detail in Chapter 7, "Foreign Language and Non-Rhoda Text".
3.1.3
Load an XML file into a web browser
Since I have created the first XML document, I just want to see it. This file can be opened directly in the Internet Explorer 5.0 that supports XML. Figure 3-2 shows the result.
The results we see will be different from different browsers. In this case, the file is formatted well, expressed different syntax in different colors. However, there is no attractive place to see. The problem is that the browser does not understand how to process the Foo element. We must indicate how the browser handles each element, this is to use the style. We will introduce it simply, but first examine this document carefully.
Figure 3-2 Show results in Hello.xml in Internet Explorer 5.0
.2 Simple XML document
Let's check this simple XML document listed in Listing 3-1 in order to better understand the meaning of each line of code. The first line is XML declaration:
This is an example of an XML processing instruction. Processing instructions
Start, and in "> end. in
The first word after the first word is the processing instruction name, in this example XML.
XML declares two features of Version and Standalone. Features are named by the aliquot of the equal sign - value pair. Located on the left side of the equal sign is the characterist name, and its value is located on the right side, and it is enclosed in double quotes.
Each XML document starts with an XML declaration to indicate the version used to the XML. In the above example, the Version property indicates that this document meets the XML 1.0 specification. The XML declaration can also have Standalone features, which tells us whether the document is in this file or needs to import file from the outside. In this example, and in several chapters, all documents are completed in a file, so the value of the Standalone feature is set to Yes.
Let us now look at the three lines below in the list 3-1:
Hello XML!
In general, these three lines form a foo elements. Separately
It is a start tag, but the end tag, Hello XML! Is the content of the foo element.
Readers may ask,
What is the meaning of the tag? Answer is "What do you want to make it?" In addition to hundreds of predefined tags, XML also allows users to create the required tags. thus
The tag can have any of the meaning of the user. The same XML document can be written in different marker names, as indicated by Listing 3-2, 3-3, and 3-4:
Listing 3-2: Greeting.xml
Hello XML!
Listing 3-3: Paragraph.xml
Hello XML!
Listing 3-4: Document.xml
Hello XML!
Listing 3-1 to 3-4 These four documents have different marking names, but they are equivalent because they have the same structure and content.
3.3 During the XML mark
There are three types of meanings: structures, semantics and patterns. The structure is divided into elemental trees. Semantics link a single element with external actual things. The style specifies how to display an element.
The structure is just the form of the document, regardless of the difference between a single marker and the elements. For example, the four XML document structures in the above lists 3-1 to 3-4 are the same. They all specify a document with a non-empty basic element. Different names of the tags do not have a structural meaning.
Semantic meaning exists outside of the document, in the heart of the author or the reader or some of the computer program that generates or reads these files. For example, understand HTML but does not understand XML web browser, may assign a paragraph meaning to
Tag, but not assigning the tag
with,
Or
with. People who speak English may
And or easier to understand
Or
Significance. As the meaning of "beautiful" exists in the hearts of the observer.
As a dumb machine, the computer cannot be said to really understand the meaning of anything. The computer only processes the bit and bytes based on a predetermined formula (although very fast). For a computer, use
Or
Use
or
There is no difference in marking. Even for web browsers, it can't be said that it understands what is paragraph. All browsers understand that when you encounter a paragraph, you should place an empty line in front of the next element.
Naturally, making the name of the mark can be better than that of itself. Many disciplines, such as mathematics and chemistry are creating the discipline industry standards and tags. These standards and tags should be used if appropriate. But in most cases, what tag is needed to create what tag.
Here are some other possible tags:
The third type of meaning that can be linked to the mark is style. Styles Meaning Specify the content of the mark on the computer screen or on other output devices. Styles Meaning Description Specific elements are bold, bertained, green
24 pound
The font is also represented by other fonts. Computers are better than understanding the semantics in understanding styles. In XML, style significance is applied by style.
.4 Writing a style list for XML documentation
XML allows users to create any tags required. Of course, since the user has a complete freedom on the creation tag, the general browser cannot expect the user's mark, and it is not possible to provide a rule for displaying these tags. Thus, the user must write a style list to the document, telling the browser how to display a specific tag. Similar to the marker set, the style of the user can share with a different document, and you can set the style you created with some of the styles written in others.
As discussed in Chapter 1, there is now more than one style single language. The Cascading Style Sheets is used herein is the Cascading Style Sheets. The advantage of CSS is that it is W
3C
The formulated standard is familiar with many people who write HTML and supported by the avant-garde's XML capabilities.
As noted in Chapter 1, another possible choice is an Extensible Style Language. XSL is currently the most powerful and flexible style language, which is especially designed to apply XML. However, XSL is more complicated than CSS and is not well supported, and it has not been completed.
XSL will be discussed in Chapter 5, 14 and 15.
The greeting.xml example in Listing 3-2 includes only one tag
Thus everything you need is to define a style for a Greeting element. Listing 3-5 is a very simple style, specifying the content of the greeting element should
24 pound
The bold is shown as a block level element.
Listing 3-5: Greeting.xsl
Greeting {display: block; font-size:
24pt
Font-weight: bold;}
Listing 3-5 should type in a text editor, saved as a new file called Greeting.css, placed in the same directory where the files in Listing 3-2. Extension. CSS represents a cascading style sheet. Again. The CSS extension is important, but the file name is not very important. If you intend to use this pattern only on an XML document, then the same file name (extension is .css rather than .xml) is often more convenient to XML. .5 Additional style is attached to the XML document
After writing a good XML document and the CSS style list for this document, you also need to tell the browser to use the style single to the document. Many different ways may be achieved for a long period of time, including browser-servers negotiate, naming the default method on the browser over the HTTP file header. However, the only effective method is to include another processing instruction in the XML document to specify the style sheet to be used.
Processing command is
And its two features, Type and HREF. The Type feature specifies the style language used, and the HREF feature specifies a URL that can be found in the style (may be relative). In Listing 3-6, the XML-Stylesheet handling instruction indicates that the style single text named Greeting.css applied to the document is written in a single language with the CSS style.
Listing 3-6: Greeting.xml with XML style single processing instructions
Hello XML!
Since we have created the first XML document and style, then of course I want to see the result. What we have to do is to load Listing 3-6 into Mozilla or Internet Explorer 5.0. Figure 3-3 is a Welcome screen displayed in Internet Explorer 5.0. Figure 3-4 is a Welcome screen showing the style in Mozilla in early development versions.
Figure 3-3 StyleDGReeting.xml file displayed in Internet Explorer 5.0
Figure 3-4 StyleDgreeting.xml file displayed in the early developer version of Mozilla
3.6 This chapter is summarized
In this chapter, readers have learned how to create a simple XML document. In general, include the following:
· ???????? How to write and save a simple XML document
How to give three types of meaning (structures, semantics and style) to XML tags
How to write CSS style orders for XML documents, telling the browser how to display a specific tag
How to attach the CSS style sheet with XML-Stylesheet processing instruction to the XML document
How to put XML into your browser
In the next chapter, we will study a larger example of the XML document, which is used to demonstrate more practical considerations when choosing an XML tag.