And so will this:. But, exactly what does this mean? Use this with CSS to minimize presentational clutter. XML Namespaces were invented to rectify a common problem: the collision of documents using identical element names for different data. Imagine you were running a bookstore and had an inventory file called inventory. A human being could probably figure out that one title has nothing to do with the other, but an application that tried to sort it out would go nuts. We need to have a way to distinguish between the two different semantic universes in which these identical terms exist.
Your inventory file stores information about books on the shelf, but the sales file stores information about books that have been bought by customers. In either situation, regardless of the chasm that lies between the contexts of these identical terms, we need a way to properly label each context. Namespaces to the rescue! To use and declare a namespace, we must first tie the namespace to a URI.
URIs can take the following forms:.
Uniform Resource Locator: a specific protocol, machine address, and file path e. For example, all published books have an ISBN. However, armed with the ISBN, you could walk into the store, ask an employee to search for you, and they could take you right to the book provided, of course, that it was in stock. We want to use our namespace throughout our XML documents, though, and the last thing we want to do is type out an entire URI every time we need to distinguish one context from another.
So, we define a prefix to represent our namespace to ease the strain on our typing fingers:. The agreed way to do that is to prefix the namespace declaration with xmlns :, like this:. At this point, we have something useful. If we needed to, we could add our prefix to appropriate elements to disambiguate I love that term! In most cases, placing your namespace declarations will be rather easy. Please note, however, that namespaces have scope. Namespaces affect the element in which they are declared, as well as all the child elements of that element.
However, if you want to limit your namespace scope to a certain part of a document, feel free to do so — remembering, of course, that this can get pretty tricky. It would become pretty tiresome to have to type a prefix for every single element in a document. On the other side of the coin, all XSLT elements must be given the xsl: prefix.
This document contains a root element letter that contains three other elements to , from , and message , each of which contains text. When you display your XML document, you should see something similar to Figure 2.
Figure 2. As you can see, CSS did a marvelous job of rendering a nicely shaded box around the entire letter, setting fonts, and even displaying things like margins and padding. Strictly speaking, the CSS standard does allow for this sort of thing with the content property, which can produce generated text before and after document elements. Think of it as a tool that you can use to transform your XML documents into other documents.
Here are some of the possibilities:. XSLT is a rules-based, or functional language. Because XSLT can be a little bewildering even for veteran programmers, the best way to tackle it is to walk through a series of examples. Keeping both these elements simple will give us the opportunity to step through the major concepts involved. They must therefore follow the rules that apply to all XML documents: an XSL file must contain a root element, all attribute values must be quoted, and so on.
The version attribute is required. The xmlns:xsl attribute is used to declare an XML namespace with the prefix xsl. In our example, we will use an xsl prefix on all the stylesheet-related tags in our XSL documents to associate them with this namespace. The next element will be the output element, which is used to define the type of output you want from the XSL file. Now we come to the heart of XSLT — the template and apply-templates elements.
Together, these two elements make the transformations happen. Put simply, the XSLT processor for our immediate purposes, the browser starts reading the input document, looking for elements that match any of the template elements in our style sheet. When one is found, the contents of the corresponding template element tells the processor what to output before continuing its search.
Where a template contains an apply-templates element, the XSLT processor will search for XML elements contained within the current element and apply templates associated with them. The first thing we want to do is match the letter element that contains the rest of our document. This is fairly straightforward:. Were the value simply letter , the template would match letter elements throughout the document. By default, apply-templates will match not only elements, but text and even whitespace between the elements as well. XSLT processors have a set of default, or implicit templates, one of which simply outputs any text or whitespace it encounters.
Each of these templates matches one of the elements we expect to find inside the letter element: to , from , and message. In each case, we output a text label e. TO: and then use apply-templates to output the contents of the tag remember, in the absence of a select attribute that says otherwise,apply-templates will output any text contained in the tags automatically. The last thing we have to do in the XSL file is close off the stylesheet element that began the file:. Left this way, the output would look something like this:.
Each of our three main templates begins with a line break and then some whitespace before the label, which is being carried through to the output. But wait — what about the line break and whitespace that ends each template? Well by default, the XSLT standard mandates that whenever there in only whitespace including line breaks between two tags, the whitespace should be ignored.
What is PHP?
But when there is text between two tags e. TO: , then the whitespace in and around that text should be passed along to the output. The vast majority of XML books and tutorials out there completely ignore these whitespace treatment issues. Best to get a good grasp of it now, rather than waiting for insanity to set in when you least expect it. All it does is output the text it contains, even if it is just whitespace. Notice how each template now outputs its label e. TO: followed by a single space, then finishes off with a line break.
This gives us the fine control over formatting that we need when outputting a plain text file. Are we done yet? Not quite. When you view the XML document in Firefox, you should see something similar to the result pictured in Figure 2. Internet Explorer interprets the result as HTML code, even when the style sheet clearly specifies that it will output text. As a result, whitespace is collapsed and our whole document appears on one line. For this reason, it is not yet practical to rely on browser support for XSLT in a real-world website. You should see something similar to Figure 2. What happens if you need to transform your own XML document into an XML document that meets the needs of another organization or person?
Not to worry — XSLT will save the day! You see, Web browsers only supply collapsible tree formatting for XML documents without style sheets. XML documents that result from a style sheet transformation are displayed without any styling at all, or at best are treated as HTML — not at all the desired result.
There are several things that need to be added to your style sheet to signal to the browser that the document is more than a plain XML file, though. Here we have declared a default namespace for tags without prefixes in the style sheet. Next up, we can flesh out the output element to more fully describe the output document type:. Internet Explorer for Windows displays XHTML documents in Quirks Mode when this declaration is present, so by omitting it we can ensure that this browser will display it in the more desirable Standards Compliance mode.
The rest of the style sheet is as it was for the HTML output example we saw above. Now, we need to identify exactly what we need for our news items, binary files, and Web copy. We must also manage and track site administrators using XML. Compared to our article content type, news will be fairly straightforward.
We will need to track these pieces of information:. The easiest way to keep track of copy is to treat each piece a little like an article. An XML document that tracks a piece of Web copy will look like this:. We will need to keep track of each administrator on the site, as these are the folks who can log in and make changes to advertisement copy, articles, news pieces, and binary files.
After that, you should have enough of a working knowledge of XML and its wacky family to really start development. In fact, in many contexts, consistency can be a very beautiful thing. Remember that XML allows you to create any kind of language you want. In many cases, as long as you follow the rules of well-formedness, just about anything goes in XML. However, there will come a time when you need your XML document to follow some rules — to pass a validity test — and those times will require that your XML data be consistently formatted.
What we need is a way to enforce that kind of rule. 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. 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.
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 can partly be solved by:. That was 17 lines of TypoScript code and converting this information into an XML structure could look like this:. That was 35 lines of XML - the double amount of lines! And in bytes probably also much bigger. This example clearly demonstrates why not XML! XML will just get in the way, it is not handy for what TypoScript normally does.
But hopefully you can at least use this example in your understanding of what TypoScript is compared to XML. The reasonable application for using XML as an alternative solution to TypoScript is if an XML editor existed which in some way made the entering of XML data into a structure like this possible and easy. In the article that coined the term Ajax,   Jesse James Garrett explained that the following technologies are incorporated:.
Since then, however, there have been a number of developments in the technologies used in an Ajax application, and in the definition of the term Ajax itself. XML is no longer required for data interchange and, therefore, XSLT is no longer required for the manipulation of data. Many developers dislike the syntax used in the XMLHttpRequest object, so some of the following workarounds have been created.
Group of interrelated Web development techniques. Retrieved 19 June MDN Web Docs. Retrieved 27 February Beginning Ajax. Archived from the original on 5 July Retrieved 24 June Archived from the original on 23 June Retrieved 14 July Retrieved 30 November Apple Inc. Retrieved 25 June Archived from the original on 30 March Retrieved 17 May Aaron Swartz.
- How AJAX Works!
- Ajax (programming)!
- What is PHP??
- Tyrannosaurus Wrecks (Mid NFW Mayhem Book 7)!
Copyright 2019 - All Right Reserved