Belgian Waffles $ Two of our famous ."/>
This tutorial will teach you the basics of XML. The tutorial is divided into sections such as in this tutorial, please notify us at [email protected] XML Tutorial in PDF - Learn XML in simple and easy steps starting from basic to advanced concepts with examples including Overview, XML document syntax. xml version="" encoding="UTF-8"?> Belgian Waffles $ Two of our famous .
|Language:||English, Spanish, Japanese|
|Distribution:||Free* [*Registration needed]|
Basic XML Concepts. 3. „XML is the cure for your data exchange, information pdf">. Parsing XML. A Basic XML Document. Differences Between XML and HTML. Common Mistakes. White Space. Closing Tags. Nesting Tags. Root Element. An XML Tutorial. JTC1/ depend upon XML technologies in the future! . PDF. • XML Schema an alternative to a DTD and used to validate.
Examples are better than words. Examples are often easier to understand than text explanations. The Bootstrap Certificate documents your knowledge of the Bootstrap framework. JS Reference. XML was designed to store and transport data. XML was designed to be both human- and machine-readable. We recommend reading this tutorial, in the sequence listed in the left menu. What we need is a way to enforce that kind of rule. In XML, there are two ways to set up consistency rules: A DTD document type definition is a tried and true if not old-fashioned way of achieving consistency.
Each of these technologies contains lots of hidden nooks and crannies crammed with rules, exceptions, notations, and side stories. Speaking of side stories, did you know that DTD actually stands for two things? It stands not just for document type definition, but also document type declaration. The declaration consists of the lines of code that make up the definition.
Just a warning before we start this chapter: As for the first question, many possible answers spring to mind:. Using a system to ensure consistency allows your XML documents to interact with all kinds of applications, contexts, and business systems — not just your own.
The way DTDs work is relatively simple. A DTD might look something like this:.
Those of you who are paying attention should have noticed some remarkable similarities between this DTD and the Letter to Mother example that we worked on in Chapter 2, XML in Practice. In fact, if you look closely, each line of the DTD provides a clue as to how our letter should be structured.
This is called an element declaration. You can declare elements in any order you want, but they must all be declared in the DTD. A DTD element declaration consists of a tag name and a definition in parentheses. These parentheses can contain rules for any of:. In this case, we want the letter element to contain, in order, the elements to , from , and message.
As you can see, the sequence of child elements is comma-delimited. In fact, to be more precise, the sequence not only specifies the order in which the elements should appear, but also, how many of each element should appear. In this case, the element declaration specifies that one of each element must appear in the sequence.
If our file contained two from elements, for example, it would be as invalid as if it listed the message element before to. How will you do that? With a neat little system of notation, defined in Table 3. After the letter declaration, we see these three declarations: So whenever you see this notation in a DTD, you know that the element must contain only text.
This notation allows the paragraph element to contain any combination of plain text and b , i , u , and highpriority elements. Note that with mixed content like this, you have no control over the number or order of the elements that are used. What about elements such as the hr and br , which in HTML contain no content at all?
These are called empty elements, and are declared in a DTD as follows:. Remember attributes? An attribute declaration is structured differently than an element declaration.
For one thing, we define it with! Also, we must include in the declaration the name of the element that contains the attribute s , followed by a list of the attributes and their possible values. Basically, this attribute can contain any string of characters or numbers.
In DTD-speak, this means that the attribute is optional. Instead of allowing any arbitrary text, however, the DTD limits the values to either male or female.
If, in our document, an actor element fails to contain a gender attribute, or contains a gender attribute with values other than male or female , then our document would be deemed invalid.
The actorid attribute has been designated an ID. In DTD-speak, an ID attribute must contain a unique value, which is handy for product codes, database keys, and other identifying factors. In our example, we want the actorid attribute to uniquely identify each actor in the list. The ID type set for the actorid attribute ensures that our XML document is valid if and only if a unique actorid is assigned to each actor.
Incidentally, if you want to declare an attribute that must contain a reference to a unique ID that is assigned to an element somewhere in the document, you can declare it with the IDREF attribute type. An entity is a piece of XML code that can be used and reused in a document with an entity reference. There are different types of entities, including general, parameter, and external. General entities are basically used as substitutes for commonly-used segments of XML code.
For example, here is an entity declaration that holds the copyright information for a company:. Parameter entities are both defined and referenced within DTDs. What this says is that each of the elements paragraph , intro , sidebar , and note can contain regular text as well as b , i , u , citation , and dialog elements.
Not only does the use of a parameter entity reduce typing, it also simplifies maintenance of the DTD. External entities point to external information that can be copied into your XML document at runtime. For example, you could include a stock ticker, inventory list, or other file, using an external entity. An external DTD is usually a file with a file extension of. First, you must edit the XML declaration to include the attribute.
This will search for the letter. If the DTD lives on a Web server, you might point to that instead:. Finally, XML Schema provides very fine control over the kinds of data contained in an element or attribute. Now, for some major drawbacks: Most of the criticism aimed at XML Schema is focused on its complexity and length. Okay, now you know a lot more about DTDs than you did before. The first thing you do is you take a look at the dozens of corporate memos you and your colleagues have received in the past few months.
After a day or two of close examination, a pattern emerges.
Although your first impulse might be to run out and create a sample XML memo document, please resist that urge for now. Because these memos are internal to the company, and there may be a need for a separate external memo DOCTYPE, you decide to use internalmemo as your root element name:.
The first element — the root element — is internalmemo. This element will contain all the other elements, which hold date, sender, recipient, subject line, and all other information. Because these represent a lot of elements, it would be useful to split your document into two logical partitions: The header will contain recipient, subject line, date, and other information. The body will contain the actual text of the memo.
In DTD syntax, the above declaration states that our internalmemo element must contain one header element and one body element. Next, we will indicate which elements these will contain.
In DTD syntax, the above declaration states that the header element must contain single date , sender , and recipients elements, an optional blind-recipients element, and then a subject element.
In DTD syntax, the above declaration states that the body element must contain one or more para elements, followed by a single sig element.
Most of the other elements will contain plain text, except the para elements, in which we will allow bold and italic text formatting. That was simple enough.
Those pieces of information are hardly ever displayed on a document — they are used only for administrative purposes. In any case, we want to be able to control the data that document creators put in for values such as priority. The best way to store these pieces of information is to add them as attributes to the root element.
To do that, we need to add an attribute declaration to our DTD:. The result should look a lot like Figure 3. Do you see how, under Results, it reads No errors or warnings found.? In Dreamweaver MX , the results list for a valid document is simply empty, and the status bar beneath the list reads Complete. What happens if some things are out of place? What would happen then?
Notice that Dreamweaver MX tells you where the problem lies with a specific line number and provides a description of the problem. The validator catches that too, as you can see in Figure 3. Figure 3.
Error resulting from a misplaced element. Again, the validator gives you a line number and a description that can lead you to resolve the problem. All you need to do is put the sender element back in the prescribed order, and the document will validate once more. In that case, we embedded the DTD right into the file. You now have a reusable DTD that you can apply to other internal memos.
We now understand articles, news stories, binary files, and Web copy, and are well on our way to completing the requirements-gathering phase of the project — we can start coding soon! If you recall, we are tracking author, status, keyword, and other vital information in separate files. That is, each individual article, news story, binary file, and Web copy file keeps track of its own keywords, status, author, and dates.
If we wanted to display all documents for a certain author, we would have to dig through all of our files to find all the matches. Never fear — I have a proposal that will solve this problem. In fact, the rest of this chapter will be devoted to tackling this issue. With any luck, it will also give you some insights into the ways in which you can analyze requirements and come up with more architecturally sound XML designs.
The other problem is a little less obvious. To our application, these three names are different, and articles will thus be listed under three different authors. To solve this problem, we should create a separate author listing authors. Once we have this figured out, we can get rid of the author element in all the other content types, and replace them with an authorid elements. Handling our authors this way also allows us to track other information about authors, such as their email addresses, their bylines in case they want to publish under pseudonyms , and other such information.
Instead of a separate author element, we would add an authorid element to our articles, like this: All we need to do is use this author ID in our articles, news stories, and all other content we add to our CMS; this ID is used to look up the author and retrieve the information we need.
The big question remains: To be completely honest, most articles, news stories, and such will be submitted to the site through our administrative tool. This tool will have the necessary forms that will restrict data entry to certain fields.
In other words, our administrative tool will do most of the work of validating our content. However, I think it would be good practice to develop a DTD for our article content type — after all, this is one of the most important document types we have in our system, and it has to be done right.
Although we have declared our body element to contain character data, our article bodies will indeed be formatted using HTML tags. Try writing DTDs for these as well. We used it to transform an XML letter to mother into something that could be displayed in a browser window. XPath is used in a variety of applications and technologies, however, XSLT is where its power and versatility really shine. For all intents and purposes, XPath is a query language.
It uses a simple notation that is very similar to directory paths hence the name XPath. When we put together a template, we normally use XPath to establish a match. For example, we can always handle the root of an XML document like this:. With XPath, you can select all elements that have a particular tag name.
Or, you could match certain elements depending on their location within an XML file. As you can see, the basic XPath syntax looks a lot like a file path on your computer. But you can go a step further and set conditions on which elements are matched within your specified path. These conditions are called predicates , and appear within square brackets following the element name you wish to set conditions for.
The symbol identifies priority in this example as an attribute name, not a tag name. XPath also has a number of useful functions built in. For example, if you need to grab the first or last element of a series, you can use XPath to do so.
Although most practical applications are relatively simple, XPath can get quite twisty when it needs to be. The XPath Recommendation is quite a useful reference to these areas of complexity. Book chapters provide an excellent opportunity to understand the arbitrary complexity of most XML documents. From the perspective of an XML document designer, however, a book chapter can be intimidatingly complex. Chapters can have titles and sections, and those sections can have titles.
There are paragraphs throughout — some belong to the chapter for example, introductory paragraphs , but others belong to sections. Sections can contain subsections. Paragraphs can contain text in italics, bold text, and other inline markup. In fact, one could even have different types of paragraphs, like notes, warnings, and tips.
There are lots of possibilities for displaying these kinds of information. This sample file could go on and on, but I think you get the idea. The first thing we want to do is to match the root of our document.
Nothing could be simpler, right? Viewed in a browser, our output will look something like that shown in Figure 4. Of course there is. The title element near the top of the document is the chapter title, and should be handled differently from the title elements in the different nested sections.
Likewise, para elements that denote warnings or introductions should be handled differently from other paragraphs. To distinguish between these different title types, you can use XPath notation. Figure 4. What about the paragraphs?
Unlike the titles, they are not distinguishable by their placement in the document alone. Instead, the document uses the type attribute to distinguish normal paragraphs from introductions, tips, and warnings.
Luckily, XPath lets us specify matches based on attribute values, too. In XPath, we use a predicate a condition in square brackets to match an attribute value. We should definitely take advantage of this ability and distinguish each of our paragraph types visually. We can also make sure that warnings are displayed in red text. Note the priority attribute on this template. By default, XSL templates have a priority between To make sure our introductory paragraphs will use this second template, we therefore assign a priority of 1.
Example 4. How can we modify our template to display the actual chapter title in this spot instead? When you need to pull a simple piece of information out of the XML document without messing around with templates to process the element s that house it, you can use a value-of element to grab what you want with an XPath expression:.
As you can see, the select attribute is an XPath expression that searches for the value of the title within the chapter. You have just communicated between two platforms which are potentially very different! What makes XML truly powerful is the international acceptance it has received. Many individuals and corporations have put forth their hard work to make XML interfaces for databases, programming, office application, mobile phones and more.
It is because of this hard work that the tools exist to do these conversion from whatever platform into standardized XML data or convert XML into a format used by that platform. In the past, attempts at creating a standardized format for data that could be interpreted by many different platforms or applications failed miserably. XML has largely succeeded in doing this.