Sams Teach Yourself XML in 24 Hours 3rd Ed Edition Morrison: - Click The Link Below To Download
Sams Teach Yourself XML in 24 Hours 3rd Ed Edition Morrison: - Click The Link Below To Download
https://ebookfinal.com/download/sams-teach-yourself-c-in-24-hours-
complete-starter-kit-3rd-edition-sams-teach-yourself-in-24-hours-
jesse-liberty/
ebookfinal.com
https://ebookfinal.com/download/sams-teach-yourself-networking-
in-24-hours-3rd-ed-edition-habraken/
ebookfinal.com
https://ebookfinal.com/download/sams-teach-yourself-php-
in-24-hours-3rd-edition-matt-zandstra/
ebookfinal.com
https://ebookfinal.com/download/sams-teach-yourself-adobe-premiere-
pro-in-24-hours-sams-teach-yourself-1st-edition-jeff-sengstack/
ebookfinal.com
SAMS Teach Yourself Adobe Photoshop CS2 in 24 Hours 3rd
Edition Carla Rose
https://ebookfinal.com/download/sams-teach-yourself-adobe-photoshop-
cs2-in-24-hours-3rd-edition-carla-rose/
ebookfinal.com
https://ebookfinal.com/download/sams-teach-yourself-adobe-
illustrator-10-in-24-hours-peter-bauer/
ebookfinal.com
https://ebookfinal.com/download/sams-teach-yourself-dvd-authoring-
in-24-hours-1st-edition-jeff-sengstack/
ebookfinal.com
https://ebookfinal.com/download/sams-teach-yourself-macromedia-
fireworks-mx-in-24-hours-1st-edition-jackson-west/
ebookfinal.com
https://ebookfinal.com/download/sams-teach-yourself-adobe-
photoshop-7-in-24-hours-1st-edition-carla-rose/
ebookfinal.com
Sams teach yourself XML in 24 hours 3rd ed Edition
Morrison Digital Instant Download
Author(s): Morrison, Michael
ISBN(s): 9782005933163, 2005933163
Edition: 3rd ed
File Details: PDF, 5.92 MB
Year: 2006
Language: english
Teach
Yourself
XML
Michael Morrison
in 24
Hours
THIRD EDITION
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered
in quantity for bulk purchases or special sales. For more information,
please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearsoned.com
Contents at a Glance
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Table of Contents
Introduction 1
How This Book Is Structured . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
How to Use This Book ............................................................................4
Naming Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
HOUR 13: Access Your iTunes Music Library via XML 271
The Role of XML in iTunes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
HOUR 23: Going Wireless with WML and XHTML Mobile 471
XML and the Wireless Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
WML Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Creating WML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
HOUR 24: Syndicating the Web with RSS News Feeds 499
A Brief History of RSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Using an RSS News Aggregator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Index 525
About the Author
Michael Morrison is a writer, developer, toy inventor, and author of a variety of technolo-
gy books, including BlackBerry In a Snap, Beginning Mobile Phone Game Programming, and
Teach Yourself HTML and CSS in 24 Hours. In addition to his primary profession as a writer
and freelance nerd for hire, Michael is the creative lead at Stalefish Labs, an entertainment
company he co-founded with his wife, Masheed. The first web-based project for Stalefish
Labs is an online music game called Guess That Groove (http://www.guessthatgroove.com/).
When not glued to his computer, playing hockey, skateboarding, or watching movies with
his wife, Michael enjoys hanging out by his koi pond. You can visit Michael on the web
and discuss this book at http://www.michaelmorrison.com/.
Dedication
Acknowledgments
Thanks to Loretta Yates, Mark Renfrow, Charlotte Clapp, Matt Purcell, and the rest of the
gang at Sams Publishing for giving me yet another writing opportunity, and for being
such a pleasure to work with, as always.
A big thanks also goes to my buddy Stephen Tallent, who graciously offered up his
considerable XML knowledge in the wee hours of the night as I peppered him with
email questions.
And finally, the biggest thanks of all goes to my wonderful family and friends.
We Want to Hear from You!
As the reader of this book, you are our most important critic and commentator. We value
your opinion and want to know what we’re doing right, what we could do better, what
areas you’d like to see us publish in, and any other words of wisdom you’re willing to
pass our way.
As publisher for Sams Publishing, I welcome your comments. You can email or write me
directly to let me know what you did or didn’t like about this book—as well as what we can
do to make our books better.
Please note that I cannot help you with technical problems related to the topic of this book. We
do have a User Services group, however, where I will forward specific technical questions related
to the book.
When you write, please be sure to include this book’s title and author as well as your name,
email address, and phone number. I will carefully review your comments and share them
with the author and editors who worked on the book.
Email: feedback@samspublishing.com
For more information about this book or another Sams Publishing title, visit our website at
www.samspublishing.com. Type the ISBN (excluding hyphens) or the title of a book in the
Search field to find the page you’re looking for.
This page intentionally left blank
Introduction
Of all the software technologies that have come and gone in the relatively short time since
we all plugged in to the Web, few have been as far-reaching yet misunderstood as XML.
Even with its catchy name that conjures up images of extreme sports, the true nature of
XML continues to elude many technical people. The reason has to do with the fact that
XML is very much a behind-the-scenes technology that helps to ensure that data is
structured in an orderly fashion. There are very few situations where an end-user can see
XML at work in a practical application. In this way, XML is a lot like residential building
codes. When a house is built, thousands of building codes are used to guide contractors so
that the house goes up safe and sound. As a homeowner, it’s difficult to look at a finished
house and grasp how all these building codes impacted the wood, shingles, and brick that
you can see and feel. The building codes are abstract in a sense that you can’t touch them,
but they play a critical role in the construction process all the same. XML plays a similar
role in software, including web sites, operating systems, and distributed applications.
I often hear people describe XML as “the new HTML,” which sounds good but is not very
accurate. XML, unlike HTML, is an extremely broad data-structuring standard that has impli-
cations far beyond web pages. For example, consider this question: HTML is to web pages as
XML is to what? This is a difficult question to answer because XML isn’t really geared toward
any one solution. Instead, XML provides the framework for creating customized solutions to a
wide range of problems. This is made possible through XML-based markup languages, which
are custom markup languages that you create using XML. If you want to chart the statistics
of your child’s baseball team, you could create your own Little League Markup Language, or
LLML, which includes custom tags and attributes for keeping up with important stats such as
hits, runs, errors, and parental outbursts. The high degree of structure in your Little League
data would allow it to be easily sorted, manipulated, and displayed according to your needs;
the data would have the mathematical flexibility of a spreadsheet along with the visual
accessibility of a web page. XML makes all this possible.
Maybe you have bigger plans for your XML knowledge than just tracking stats for a Little
League team. If so, you’ll be glad to know that XML is the enabling technology behind all
kinds of interesting software applications. Practically all of the big Internet players have
invested heavily in XML. As an example, Amazon.com uses XML to expose its product data
so that developers can build custom shopping applications. Another interesting application
of XML that has caused quite a stir recently is Google Maps, which is Google’s innovative
online mapping application. Google Maps relies on XML for map data. In fact, in Hour
15 of this book, “Using XML to Hack Google Maps,” you learn how to “hack” Google Maps
2
Teach Yourself in 24 Hours
to use your own XML-based maps. One last example of how XML may have sneakily
entered your life already is iTunes, Apple’s incredibly popular online music store. iTunes
uses XML to store information about your music library locally on your computer. With
a little bit of effort, you can access your iTunes music library via XML and view or
manipulate it any way you choose. This task is covered in Hour 13, “Access Your iTunes
Music Library via XML.”
XML is worth learning because it is an excellent back-end technology for storing and
sharing data in a highly structured manner. Another reason for learning XML has to do
much more directly with the web: XML is very much shaping the future of HTML. As you
may know, HTML is somewhat unstructured in the sense that web developers take great
liberties with how they use HTML code. Although this isn’t entirely HTML’s fault, HTML
shares a considerable amount of the blame because it doesn’t have the structured set of
rules that are part of XML. In an attempt to add structure and consistency to the Web,
a reformulated version of HTML known as XHTML was created that adds the structure of
XML to HTML. It may still be quite a while before XHTML fully unseats HTML, but web
developers are busy making the move to a more structured Web thanks to XHTML.
This book, in many ways, is a testament to the fact that XML is a technology for both the
present and the future. The majority of the book focuses on XML in the present and how it
can be used to do interesting things today. My goal was to strike a careful balance between
giving you practical knowledge for the present along with some foreshadowing of what
might lie ahead for XML.
. Part I, “XML Essentials”—In this part, you get to know the XML language and
what it has to offer in terms of structuring data. You also learn how to create XML
documents.
. Part II, “Defining XML Data”—In this part, you will find out how to define the
structure of XML documents using schemas. You learn about the two major types of
schemas (DTDs and XSDs), as well as how to use namespaces and how to validate
XML documents. You even learn about a real XML language, SVG, along with how
to use it to create vector graphics for the web.
3
Introduction
. Part III, “Formatting and Displaying XML Documents”—In this part, you will learn
how to format XML content with style sheets so that it can be displayed. XML
formatting is explored using several different style sheet technologies—CSS, XSLT,
and XSL-FO. This part of the book also guides you through some interesting practical
applications, including how to access your iTunes music library using XML and how
to create your own XML-based maps for Google Maps.
. Part IV, “Processing and Managing XML Data”—In this part, you will find out how to
process XML documents and manipulate their contents using the Document Object
Model (DOM), which provides access to the inner workings of XML documents. You
will also learn about SAX, which is a popular programming interface used to access
XML documents. Databases are also tackled, including how to access data from a
database via XML. And finally, this part of the book shows you how to use XML to
mine Google for information.
. Part V, “XML’s Impact on HTML”—In this part, you will explore XML’s relationship to
HTML and the Web. You will learn about XHTML, which is the merger of XML
and HTML, along with advanced XML linking technologies. You will also learn how
XML is being used to provide a means of creating web pages for wireless devices via
a language called WML, as well as how to syndicate the web using RSS news feeds.
In addition to web browsers, there are a few other tools mentioned throughout the book
that you may consider downloading or purchasing based upon your individual needs. At
the very least, you’ll need a good text editor to edit XML documents. Windows Notepad is
sufficient if you’re working in a Windows environment, and I’m sure you can find a suit-
able equivalent for other environments. If you want to check into a more full-featured XML
editor, it certainly won’t hurt you. I mention several editors to consider in Hour 2 of the
book, “Creating XML Documents.” That’s really all you need; a web browser and a trusty
editor will carry you a long way toward becoming proficient in XML.
4
Teach Yourself in 24 Hours
This book uses different typefaces to differentiate between code and regular English. Text
that you type and text that appears on your screen is presented in monospace type.
It will look like this to mimic the way text looks on your screen.
Placeholders for variables and expressions appear in monospace italic font. You should
replace the placeholder with the specific value it represents.
By the Notes provide you with comments and asides about the topic at hand.
Way
PART I
XML Essentials
As you undoubtedly know, the World Wide Web has grown in leaps and bounds in the past
several years, both in magnitude and in technologies. The fact that people who, only
a few short years ago had no interest in computers are now “net junkies”, is a testament
to how quickly the Web has infiltrated modern culture. Just as the usefulness and appeal of
the Web have grown rapidly, so have the technologies that make the Web possible. It all
pretty much started with HTML (HyperText Markup Language), but a long list of acronyms,
buzzwords, pipedreams, and even a few killer technologies have since followed. XML
(eXtensible Markup Language) is one of the rare technologies that actually progressed from
bleeding edge hype to misunderstood buzzword to standard building block. XML has
officially arrived, and is used behind the scenes in countless applications and web services.
Even so, I’ll leave it to you to decide whether it is truly a killer technology as you progress
through this book.
XML’s usage is continuing to grow quickly as both individuals and companies realize its
potential. However, in many ways XML is still a relatively new technology, and many
people, possibly you, are just now learning what it can do for them. Unlike some other
software technologies such as HTML or even Java, XML is a little fuzzier in terms of how it
is applied in different scenarios. Just as it’s difficult to look at a person and grasp how their
DNA makes them who they are, it can also be challenging to look at an application and
get a grasp for how XML fits into the equation. This hour introduces you to XML and gives
you some insight as to why it was created and what it can do.
By the By the way, you might as well get used to seeing loads of acronyms. Virtually every
Way technology associated with XML has its own acronym, so it’s impossible to learn
about XML without getting to know a few dozen acronyms. Don’t worry, I’ll break
them to you gently!
HTML was great in its early days because it allowed scientists to share information
over the Internet in an efficient and relatively structured manner. It wasn’t until
later that HTML started to become an all-encompassing formatting and display
language for web pages. It didn’t take long before web browsers caught on and
HTML started being used to code more than scientific papers. HTML quickly went
from a tidy little markup language for researchers to a full-blown online publishing
The What and Why of XML 9
language. And once it was established that HTML could be jazzed up simply by
adding new tags, the creators of web browsers pretty much went crazy by adding lots
of nifty features to the language. Although these new features were neat at first, they
compromised the simplicity of HTML and introduced lots of inconsistencies when it
came to how browsers rendered web pages. HTML had started to resemble a bad
remodeling job on a house that really should’ve been left alone.
As with most revolutions, the birth of the Web was very chaotic, and the modifica-
tions to HTML reflected that chaos. More recently, a significant effort has been made
to reel in the inconsistencies of HTML and to attempt to restore some order to the
language. The problem with disorder in HTML is that web browsers have to guess
at how a page is to be displayed, which is not a good thing. Ideally, a web page
designer should be able to define exactly how a page is to look and have it look
the same regardless of what kind of browser or operating system someone is using.
This utopia is still off in the future somewhere, but XML is playing a significant role
in leading us toward it, and significant progress has been made.
Throughout this book you will learn about several of the more intriguing markup By the
languages that are based on XML. For example, you will find out about SVG and Way
RSS, which allow you to create vector graphics and syndicate news feeds from web
sites, respectively.
You might be thinking that virtual pets don’t necessarily have anything to do with
the Web, so why mention them? The reason is because XML is not entirely about
web pages. In fact, XML in the purest sense really has nothing to do with the Web,
and can be used to represent any kind of information on any kind of computer. If
you can visualize all the information whizzing around the globe between computers,
10 HOUR 1: Getting to Know XML
mobile phones, televisions, and radios, you can start to understand why XML has
much broader ramifications than just cleaning up web pages. However, one of the
first applications of XML is to restore some order to the Web, which is why I’ve
provided an explanation of XML with the Web in mind. Besides, one of the main
benefits of XML is the ability to develop XML documents once and then have them
viewable on a range of devices, such as desktop computers, handheld computers,
mobile phones, and Internet appliances.
One of the really awesome things about XML is that it looks very familiar to anyone
who has used HTML to create web pages. Going back to our virtual pet example,
check out the following XML code, which reveals what a hypothetical VPML
document might look like:
<pets>
<pet name=”Maximillian” type=”pot bellied pig” age=”3”>
<friend name=”Augustus”/>
<friend name=”Nigel”/>
</pet>
<pet name=”Augustus” type=”goat” age=”2”>
<friend name=”Maximillian”/>
</pet>
<pet name=”Nigel” type=”chipmunk” age=”2”>
<friend name=”Maximillian”/>
</pet>
</pets>
This XML (VPML) code includes three virtual pets: Maximillian the pot-bellied pig,
Augustus the goat, and Nigel the chipmunk. If you study the code, you’ll notice that
tags are used to describe the virtual pets much as tags are used in HTML code to
describe web pages. However, in this example the tags are unique to the VPML
language. It’s not too hard to understand the meaning of the code, thanks to the
descriptive tags. In fact, an important design parameter of XML was for XML content
to always be human-readable. By studying the VPML code for a few seconds, it
becomes apparent that Maximillian is friends with both Augustus and Nigel, but
Augustus and Nigel aren’t friends with each other. Maybe it’s because they are the
same age, or maybe it’s just that Maximillian is a particularly friendly pig. Either
way, the code describes several pets along with the relationships between them. This
is a good example of the flexibility of the XML language. Keep in mind that you
could create a virtual pet application that used VPML to share information with
other virtual pet owners.
By the Unlike HTML, which consists of a predefined set of tags such as <head>, <body>,
Way and <p>, XML allows you to create custom markup languages with tags that are
unique to a certain type of data, such as virtual pets.
The What and Why of XML 11
The virtual pet example demonstrates how flexible XML is in solving data structur-
ing problems. Unlike a traditional database, XML data is pure text, which means it
can be processed and manipulated very easily, in addition to being readable by
people. For example, you can open up any XML document in a text editor such as
Windows Notepad (or TextEdit on Macintosh computers) and view or edit the code.
The fact that XML is pure text also makes it very easy for applications to transfer
data between one another, across networks, and also across different computing
platforms such as Windows, Macintosh, and Linux. XML essentially establishes
a platform-neutral means of structuring data, which is ideal for networked
applications, including web-based applications.
XML isn’t just for web-based applications, however. As an example, the entire By the
Microsoft Office line of products use XML under the hood to store and share Way
document data.
Earlier in the hour I mentioned that the main problem with HTML is that it got
somewhat messy and unstructured, resulting in a lot of confusion surrounding the
manner in which web browsers render web pages. To better understand XML and its
relationship to HTML, you need to know why HTML has gotten messy. HTML was
originally designed as a means of sharing written ideas among scientific researchers.
I say “written ideas” because there were no graphics or images in the early versions
of HTML. So, in its inception, HTML was never intended to support fancy graphics,
formatting, or page-layout features. Instead, HTML was intended to focus on the
meaning of information, or the content of information. It wasn’t until web browser
vendors got excited that HTML was expanded to address the presentation of
information. In fact, HTML was in many ways changed to focus entirely on how
information appears, which is what ultimately prompted the creation of XML.
12 HOUR 1: Getting to Know XML
You’ll learn throughout this book that one of the main goals of XML is to separate the
meaning of information from the presentation of it. There are a variety of reasons
why this is a good idea, and they all have to do with improving the organization
and structure of information. Although presentation plays an important role in any
web site, modern web applications have evolved to become driven by data of very
specific types, such as financial transactions. HTML is a very poor markup language
for representing such data. With its support for custom markup languages, XML
makes it possible to carefully describe data and the relationships between pieces of
data. By focusing on content, XML allows you to describe the information in web
documents. More importantly, XML makes it possible to precisely describe informa-
tion that is shuttled across the Net between applications. For example, Amazon.com
uses XML to describe products on its site and allow developers to create applications
that intelligently analyze and extract information about those products.
By the You might have noticed that I’ve often used the word “document” instead of
Way “page” when referring to XML data. You can no longer think of the web as a bunch
of linked pages. Instead, you should think of it as linked documents. Although this
may seem like a picky distinction, it reveals a lot about the perception of web
content. A page is an inherently visual thing, whereas a document can be anything
ranging from a stock quote to a virtual pet to a music CD on Amazon.com.
If XML describes data better than HTML, does it mean that XML is set to upstage
HTML as the markup language of choice for the Web? Not exactly. XML is not
a replacement for HTML, or even a competitor of HTML. XML’s impact on HTML
has to do more with cleaning up HTML than it does with dramatically altering
HTML. The best way to compare XML and HTML is to remember that XML
establishes a set of strict rules that any markup language must follow. HTML is
a relatively unstructured markup language that could benefit from the rules of
XML. The natural merger of the two technologies is to make HTML adhere to the
rules and structure of XML. To accomplish this merger, a new version of HTML
has been formulated that adheres to the stricter rules of XML. The new XML-
compliant version of HTML is known as XHTML. You learn a great deal more
about XHTML in Hour 21, “Adding Structure to the Web with XHTML.” For now,
just understand that one long-term impact XML will have on the Web has to do
with cleaning up HTML.
By the Most standardized web technologies, such as HTML and XML, are overseen by the
Way W3C, or the World Wide Web Consortium, which is an organizational body that
helps to set standards for the Web. You can learn more about the W3C by visiting
its web site at http://www.w3.org/.
XML and Web Browsers 13
XML’s relationship with HTML doesn’t end with XHTML, however. Although XHTML
is a great idea that is already making web pages cleaner and more consistent for
web browsers to display, we’re a ways off from seeing a Web that consists of cleanly
structured XHTML documents (pages). It’s currently still too convenient to take
advantage of the freewheeling flexibility of the HTML language. Where XML is
making a significant immediate impact on the Web is in web-based applications
that must shuttle data across the Internet. XML is an excellent medium for
representing data that is transferred back and forth across the Internet as part of
a complete web-based application. In this way, XML is used as a behind-the-scenes
data transport language, whereas HTML is still used to display traditional web
pages to the user. This is evidence that XML and HTML can coexist happily both
now and into the future.
Another way to view XML documents is with style sheets using either XSL
(eXtensible Stylesheet Language) or CSS (Cascading Style Sheets). Style sheets have
finally reached the mainstream and are established as a better approach to
formatting web pages than many of the outdated HTML presentation tags. Style
sheets work in conjunction with HTML code to describe in more detail how HTML
data is to be displayed in a web browser. Style sheets play a similar role when used
with XML. Most modern web browsers (Internet Explorer, Firefox, Opera, Safari,
and so on) support CSS, as well as providing some level of support for XSL. You
learn a great deal more about style sheets in Part III, “Formatting and Displaying
XML Documents.”
14 HOUR 1: Getting to Know XML
By the In addition to popular commercial web browsers, the W3C offers its own open
Way source web browser that can be used to browse XML documents. The Amaya web
browser supports the editing of web documents and also serves as a decent
browser. However, Amaya is intended more as a means of testing XML documents
than as a commercially viable web browser. You can download Amaya for free from
the W3C web site at http://www.w3c.org/Amaya/.
One last point to make in regard to viewing XML with web browsers is that some
browsers allow you to view XML code directly. This is a neat feature because it
automatically highlights the code so that the tags and data are easy to see and
understand. Additionally, an XML document is usually displayed as a hierarchical
tree that allows you to expand and collapse sections of the data just as you expand
and collapse folders in a file manager such as Windows Explorer. This hierarchical
user interface reveals the tree-like structure of XML documents. Figure 1.1 shows the
virtual pets XML document as viewed in Internet Explorer.
FIGURE 1.1
You can view
the code for an
XML document
by opening the
document in a
web browser
that supports
XML, such as
Internet
Explorer.
Real-World XML 15
Although this black and white figure doesn’t reveal it, Internet Explorer actually uses
color to help distinguish the different pieces of information in the document. To expand
or collapse a section in the document, just click anywhere on the tag. Figure 1.2 shows
the document with the first pet element (Maximillian) collapsed; notice that the minus
sign changes to a plus sign (+) to indicate that the element can be expanded.
FIGURE 1.2
In addition to
highlighting
XML code for
easier viewing,
XML-supported
web browsers
make it possi-
ble to expand
and collapse
sections of
a document.
Keep in mind that the web browser in this case is only showing the XML document as
a tree of data because it doesn’t know anything else about how to render it. You can
provide a style sheet that lays out the specifics of how the data is to be formatted and
displayed, and the browser will carefully format the data instead of displaying it as a
tree. You will tackle this topic in Part III. This approach is commonly used to style XML
data for viewing on the Web. Even so, it can be handy opening an XML document in
a browser without any styling applied (as shown in Figures 1.1 and 1.2) and studying it
as a tree of data. Although browsers provide a neat approach to viewing XML code in
a tree-like structure, you’ll probably rely on an XML editor to view most of the XML
code that you develop. Or you can use a simple text editor such as Windows Notepad.
You learn about XML editors in the next hour, “Creating XML Documents.”
Real-World XML
Hopefully by now you understand some of the reasons XML came into being, as
well as how it will likely fit in with HTML as the future of the web unfolds. What
I haven’t explained yet is how XML is impacting the real world with new markup
16 HOUR 1: Getting to Know XML
languages. Fortunately, a lot of work has been done to make XML a technology that
you can put to work immediately, and there are numerous XML-related technologies
that are being introduced as I write this. Following is a list of some of the major
XML-based languages that are supported either on the web or in major XML-based
applications, along with the kinds of information they represent:
. RSS (Really Simple Syndication)—Syndicated web site updates (news feeds and
blog entries, for example)
I told you earlier that XML people love acronyms! And as the brief descriptions of
each language suggest, these XML languages are as varied as their acronyms. A few
of these languages are supported in the latest web browsers, and the remaining
Summary 17
languages have special applications that can be used to create and share data in
each respective format. To give you an idea regarding how these languages are
impacting the real world, consider the fact that the next major release of the
Windows operating system, codenamed Longhorn, is using XAML (pronounced
“zammel”) throughout to describe its user interfaces in XML. Additionally, Microsoft
and Intuit have invested heavily in OFX (Open Financial eXchange) as the future of
electronic financial transactions. OFX is already supported by more than 2,000
banks and brokerages, in addition to payroll-processing companies. In other words,
your paycheck may already depend on XML!
Another interesting usage of an XML language is SVG, which is used to code plats By the
for real estate. A plat is an overhead map that shows how property is divided. Plats
Way
play an important role in determining divisions of land for ownership (and taxation)
purposes and comprise the tax maps that are managed by the property tax
assessor’s office in each county in the U.S. SVG is actually much more broad than
just real estate plats and allows you to create virtually any vector graphics in XML.
You learn more about SVG in Hour 6, “Using SVG to Draw Scalable Graphics.”
I could go on and on about how different XML languages are infiltrating the real
world, but I think you get the idea. You’ll get to know several of the languages
listed throughout the remainder of the book. More specifically, Hour 23, “Going
Wireless with WML and XHTML Mobile,” shows you how to code web pages for
mobile devices, while Hour 24, “Syndicating the Web with News Feeds via RSS,”
shows you how to use the RSS language to efficiently stay up to date with your
favorite web sites.
As more evidence of the importance that major technology players have placed By the
on XML, consider the fact that Microsoft’s .NET development platform is based Way
entirely upon XML.
Summary
Although it doesn’t solve all of the world’s problems, XML has a lot to offer the
web community and the computing world as a whole. Not only does XML
represent a path toward a cleaner and more structured HTML, it also serves as
an excellent means of transporting data in a consistent format that is readily
accessible across networks and different computing platforms. A variety of
different XML-based languages are available for storing different kinds of
information, ranging from financial transactions to mathematical equations to
multimedia presentations.
18 HOUR 1: Getting to Know XML
This hour introduced you to XML and helped to explain how it came to be as well as
how it fits into the future of the Web. You also learned that XML has considerable
value beyond its impact on HTML. This hour, although admittedly not very hands
on, has given you enough knowledge of XML to allow you to hit the ground running
and begin creating your own XML documents in the next hour.
Q&A
Q. Why isn’t it possible to create custom tags in HTML, as you can in XML?
A. HTML is a markup language that consists of a predefined set of tags that each
has a special meaning to web browsers. If you were able to create custom tags
in HTML, web browsers wouldn’t know what to do with them. XML, on the
other hand, isn’t necessarily tied to web browsers, and therefore has no notion
of a predefined set of tags. When you do tie an XML document to a web
browser, you typically couple it with a style sheet that styles or transforms the
XML data into a format more easily displayed in the browser.
Workshop
The Workshop is designed to help you anticipate possible questions, review what
you’ve learned, and begin learning how to put your knowledge into practice.
Quiz
1. What is meant by the description of XML as a meta-language?
2. What is XHTML?
Workshop 19
Quiz Answers
1. When XML is referred to as a meta-language, it means that XML is a language
used to create other markup languages. Similar to a meta-language is meta-
data, which is data that is used to describe other data. XML relies heavily on
metadata to add meaning to the content in XML documents. RDF and OWL
are examples of XML vocabularies that expand on the concept of metadata by
attempting to add meaning to web pages.
2. XHTML is the XML-compliant version of HTML, which you will learn about in
Hour 21.
3. Most standardized web technologies, such as HTML and XML, are overseen by
the World Wide Web Consortium, or W3C, which is an organizational body
that helps to set standards for the Web.
Exercises
1. Consider how you might construct a custom markup language for data of your
own. Do you have a collection of movie posters you’d like to store in XML, or
how about your Wiffle ball team’s stats? What kind of custom tags would you
use to code this data?
2. Visit the W3C Web site at http://www.w3.org/ and browse around to get a feel
for the different web technologies overseen by the W3C.
This page intentionally left blank
HOUR 2
Creating XML Documents
Plain Old Text still reigns supreme, despite the many predictions of its demise.
—Bob Foster
Similar to HTML, XML is a technology that is best understood by working with it. I could
go on and on for pages about the philosophical ramifications of XML, but in the end I’m
sure you just want to know what you can do with it. Most of your XML work will consist of
developing XML documents, which are sort of like HTML web pages, at least in terms of
how the code is generally structured. Keep in mind, however, that XML documents can be
used to store any kind of information. After you’ve created an XML document, you will no
doubt want to see how it appears in a web browser or how it is used in a functioning
application. Because there is no standard approach to viewing an XML document according
to its meaning, you must either find or develop a custom application for viewing the
document or use a style sheet to view the document in a web browser. This hour uses the
latter approach to provide a simple view of an XML document that you create.
Of course, if you don’t have any experience with HTML you probably won’t even
realize that XML is a somewhat rigid language.
In this example, there is no data appearing between the opening and closing tags,
which illustrates that the data is indeed optional. XML doesn’t care too much about
how whitespace appears between tags, so it’s perfectly acceptable to place tags
together on the same line:
<pet></pet>
By the It’s important to note that an element is a logical unit of information in an XML
Way document, whereas a tag is a specific piece of XML code that comprises an
element. That’s why I always refer to an element by its name, such as pet, whereas
tags are always referenced just as they appear in code, such as <pet> or </pet>.
You’re probably wondering why this code broke the rule requiring that every element
has to consist of both an opening and a closing tag. In other words, why do the
friend elements appear to involve only a single tag? The answer to this question is
that XML allows you to abbreviate empty elements. An empty element is an element
that doesn’t contain any content within its opening and closing tags. The earlier pet
A Quick XML Primer 23
examples you saw are empty elements. Because empty elements don’t contain any
content between their opening and closing tags, you can abbreviate them by using
a single tag known as an empty tag. Similar to other tags, an empty tag is enclosed
by angle brackets (<>), but it also includes a forward slash (/) just before the closing
angle bracket. So, the empty friend element, which would normally be coded as
<friend></friend> can be abbreviated as <friend />. The space before the />
isn’t necessary but is a standard style practice among XML developers.
Any discussion of opening and closing tags wouldn’t be complete without pointing By the
out a glaring flaw that appears in most HTML documents. I’m referring to the <p> Way
tag, which is used to enclose a paragraph of text, and is often found in HTML
documents with an opening tag but no closing tag. The p element in HTML is not
an empty element, and therefore should always have a </p> closing tag, but most
HTML developers make the mistake of leaving it out. This kind of freewheeling
coding will get you in trouble quickly with XML!
All this talk of empty elements brings to mind the question of why you’d want to
use an element that has no content. The reason for this is because you can still
attach attributes to empty elements. Attributes are small pieces of information that
appear within an element’s opening tag. An attribute consists of an attribute name
and a corresponding attribute value, which are separated by an equal symbol (=).
The value of an attribute appears to the right of the equal symbol and must appear
within quotes. Following is an example of an attribute named name that is
associated with the friend element:
<friend name=”Augustus” />
Attributes represent another area where HTML code is often in error, at least from By the
the perspective of XML. HTML attributes are regularly used without quotes, which Way
is a clear violation of XML syntax. Always quoting attribute values is another habit
you’ll need to learn if you’re making the progression from free-spirited HTML
designer to ruthlessly efficient XML coder.
In this example, the name attribute is used to identify the name of a friend. Attributes
aren’t limited to empty elements—they are just as useful with nonempty elements.
Additionally, you can use several different attributes with a single element. Following
is an example of how several attributes are used to describe a pet in detail:
<pet name=”Maximillian” type=”pot bellied pig” age=”3”>
As you can see, attributes are a great way to tie small pieces of descriptive information
to an element without actually affecting the element’s content.
24 HOUR 2: Creating XML Documents
Inside an Element
A nonempty element is an element that contains content within its opening and
closing tags. Earlier I mentioned that this content could be either text or additional
elements. When elements are contained within other elements, they are known as
nested elements. To understand how nested elements work, consider an apartment
building. Individual apartments are contained within the building, whereas individ-
ual rooms are contained within each apartment. Within each room there may be
pieces of furniture that in turn are used to store belongings. In XML terms, the
belongings are nested in the furniture, which is nested in the rooms, which are
nested in the apartments, which are nested in the apartment building. Listing 2.1
shows how the apartment building might be coded in XML.
If you study the code, you’ll notice that the different elements are nested according
to their physical locations within the building. It’s important to recognize in this
example that the belonging elements are empty elements (lines 5–7), which is
evident by the fact that they use the abbreviated empty tag ending in />. These
elements are empty because they aren’t required to house (no pun intended!) any
additional information. In other words, it is sufficient to describe the belonging
elements solely through attributes.
It’s important to realize that nonempty elements aren’t just used for nesting purposes.
Nonempty elements often contain text content, which appears between the opening
and closing tags. Following is an example of how you might decide to expand the
belonging element so that it isn’t empty:
<furniture type=”desk”>
<belonging type=”letter”>
Dear Michael,
I am pleased to announce that you may have won our sweepstakes. You are
one of the lucky finalists in your area, and if you would just purchase
five or more magazine subscriptions then you may eventually win some
money. Or not.
</belonging>
</furniture>
A Quick XML Primer 25
In this example, my ticket to an early retirement appears as text within the belonging
element. You can include just about any text you want in an element, with the
exception of a few special symbols, which you learn about a little later in the hour.
2. Every opening tag must have a corresponding closing tag (unless it is abbrevi-
ated as an empty tag).
Admittedly, the last rule is one that I haven’t prepared you for, but the others should
make sense to you. First off, rule number one states that XML is a case-sensitive
language, which means that <pet>, <Pet>, and <PET> are all different tags. If you’re
coming from the world of HTML, this is a very critical difference between XML and
HTML. It’s not uncommon to see HTML code that alternates back and forth between
tags such as <b> and <B> for bold text. In XML, this mixing of case is a clear no-no.
Generally speaking, XML standards encourage developers to use either lowercase
tags or mixed case tags, as opposed to the uppercase tags commonly found in HTML
web pages. The same rule applies to attributes. If you’re writing XML code in a spe-
cific XML-based markup language, the language itself will dictate what case you
should use for tags and attributes.
The second rule reinforces what you’ve already learned by stating that every open-
ing tag (<pet>) must have a corresponding closing tag (</pet>). In other words, tags
must always appear in pairs (<pet></pet>). Of course, the exception to this rule is
the empty tag, which serves as an abbreviated form of a tag pair (<pet />). Rule
three continues to nail down the relationship between tags by stating that tag pairs
Exploring the Variety of Random
Documents with Different Content
his cannibal's thought-pattern, torrents of perfectly stable electricity,
slowly influencing his own pattern as he had expected to influence
the impulse, influencing his into a frozen, stable, inflexible pattern of
continuous hunger-satisfaction.
His crystalloid jaws spread wide, Rahll floated in the black void of
space, motionless, unable to move, unable to twist into new thought-
patterns, and therefore unable to think. The inflexible current of
electricity poured steadily into his body....
Slowly the effects of the morphine wore off, and the haze lifted from
Brenner's mind. As his power to think returned, he began to realize
that his plan had worked, and that the alien had been overcome; and
he was glad of this only because it removed him, Brenner, from
danger. He no longer cared that the creature might have absorbed
the impulses of every living being in existence; the thing was frozen,
and he was safe from it—that was all that mattered to him.
Of course he could not see, or feel, or hear the motionless crystalloid
pattern outside. But he knew it was there. He could sense it.
He could sense it because his heretofore unmethodical hub-impulse
pattern, in its close association with Rahll's, had been slowly twisted
and molded into a form very much like the alien's, giving him Rahll's
powers of sightless observation of other impulses and objects.
Brenner no longer had any need for his lost powers of sight, hearing,
taste, smell and touch.
He groped his way back to the nose seat, sensing his way as he
could not feel it. He would find Base—he would be able to sense the
way back to it as soon as it was close enough. He would go back to
Base, because, naturally, his basic thought-form had also been
molded into the shape of Rahll's; and Brenner was hungry, and he
knew he could find food at Base.
Food. He pictured Hale, and the other men at the station.
Food. His jagged cannibal's pattern contracted in ecstacy at the
thought of it.
He activated the atomic generator and whirled the ship around.
THE END
*** END OF THE PROJECT GUTENBERG EBOOK PATTERN ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com