Introduction to XML. Lesson 2: Yo ur First XML Do cument Phonebook XML The Tree Structure of XML

Introduction to XML Lesso n 1: Int ro duct io n t o XML Ho w to Learn Using O'Reilly Scho o l o f Techno lo gy Co urses Setting XML Mo de What is XML?...
Author: Anabel Phillips
18 downloads 0 Views 998KB Size
Introduction to XML Lesso n 1: Int ro duct io n t o XML Ho w to Learn Using O'Reilly Scho o l o f Techno lo gy Co urses Setting XML Mo de What is XML? A Histo ry o f XML and SGML Ho w is XML Being Used? Parsing XML A Basic XML Do cument Differences Between XML and HTML Co mmo n Mistakes White Space Clo sing Tags Nesting Tags Ro o t Element Capitalizatio n Quo ting Attributes Lesso n 2: Yo ur First XML Do cum e nt Pho nebo o k XML The Tree Structure o f XML Elements and Attributes What is an Element? What is an Attribute? When To Use Elements and Attributes Finding and Fixing Syntax Erro rs XML Vo cabulary XML To o ls No n-Validating Parsers Validating Parsers Translating Lesso n 3: Using DT Ds (Do cum e nt T ype De f init io ns) What is a Do cument Type Definitio n? Declaring Use o f a DTD in Yo ur XML file Internal, External, o r Neither? DTD: The Specifics Entities ANY and EMPTY What is a Co ntent Mo del? Lesso n 4: Int ro duct io n t o Sche m as What is a Schema? What is a Namespace? Yo ur First Schema

Lesso n 5: Dat a T ype s Data Types Co mplex Data Types Simple Data Types String Types Dates Attributes Extensio ns Lesso n 6 : Re st rict io ns Restrictio ns Set o f Values Range o f Values Length Pattern Values Lesso n 7: Sche m a St ruct ure Names and Refs Building a Schema Substitutio ns Any Lesso n 8 : Basic XSL What is XSL? Getting the Mo st Out o f XSL Using the Translate Functio n in Co deRunner Linking XSL to yo ur XML So urce Layo ut o f an XSL Do cument and Templates Mo re Abo ut xsl:o utput xsl:template xsl:apply-templates Lesso n 9 : Advance d XSL Translating the Address Bo o k to XHTML xsl:value-o f xsl:if Basic Syntax fo r xsl:if Syntax fo r Test xsl:if with o ur Address Bo o k xsl:cho o se xsl:fo r-each template-match o r fo r-each? Lesso n 10 : XPat h What is XPath? Using XPath to Navigate an XML Do cument

Predicates Lesso n 11: Advance d XPat h Functio ns Axes Other Axes Lesso n 12: T he f ut ure o f XML XSLT 2.0 / XPath 2.0 Changes Fo und in Versio n 2.0 Data Types Output Gro uping Lesso n 13: Final Pro je ct Perso nal Info rmatio n Manager Sto ring the Data in XML Output to HTML

Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Introduction to XML Welco me to OST's Intro ductio n to XML co urse! In this co urse, yo u will learn the fundamentals o f XML fo r use with XML-enabled applicatio ns o r general web use.

Course Objectives When yo u co mplete this co urse, yo u will be able to : demo nstrate kno wledge o f XML, SGML, and HTML. find and fix syntax erro rs in XML elements and attributes. create and declare a Do cument Type Definitio n (DTD) in an XML file. build schemas, namespaces, restrictio ns, and data types in XML. perfo rm tranfo rmatio ns using advanced XSL. navigate an XML do cument with XPath. develo p a perso nal info rmatio n manager using XML, XSL, DTD and HTML. Fro m beginning to end, yo u will learn by do ing yo ur o wn XML-based pro jects. These pro jects, as well as the final pro ject, will add to yo ur po rtfo lio and will co ntribute to certificate co mpletio n. Besides a bro wser and internet co nnectio n, all so ftware is pro vided o nline by the O'Reilly Scho o l o f Techno lo gy.

How to Learn Using O'Reilly School of T echnology Courses Welco me to the O'Reilly Scho o l o f Techno lo gy (OST) XML Co urse. Since this may be yo ur first co urse with us, we'd like to tell yo u a little abo ut o ur teaching philo so phy. We believe in a hands-o n, practical appro ach to learning.

Learning with O'Reilly School of T echnology Courses As with every O'Reilly Scho o l o f Techno lo gy co urse, we'll take a user-active appro ach to learning. This means that yo u (the user) will be active! Yo u'll learn by do ing, building live pro grams, testing them and experimenting with them—hands-o n! To learn a new skill o r techno lo gy, yo u have to experiment. The mo re yo u experiment, the mo re yo u learn. Our system is designed to maximize experimentatio n and help yo u learn to learn a new skill. We'll pro gram as much as po ssible to be sure that the principles sink in and stay with yo u. Each time we discuss a new co ncept, yo u'll put it into co de and see what YOU can do with it. On o ccasio n we'll even give yo u co de that do esn't wo rk, so yo u can see co mmo n mistakes and ho w to reco ver fro m them. Making mistakes is actually ano ther go o d way to learn. Abo ve all, we want to help yo u to learn to learn. We give yo u the to o ls to take co ntro l o f yo ur o wn learning experience. When yo u co mplete an OST co urse, yo u kno w the subject matter, and yo u kno w ho w to expand yo ur kno wledge, so yo u can handle changes like so ftware and o perating system updates. Here are so me tips fo r using O'Reilly Scho o l o f Techno lo gy co urses effectively: T ype t he co de . Resist the temptatio n to cut and paste the example co de we give yo u. Typing the co de actually gives yo u a feel fo r the pro gramming task. Then play aro und with the examples to find o ut what else yo u can make them do , and to check yo ur understanding. It's highly unlikely yo u'll break anything by experimentatio n. If yo u do break so mething, that's an indicatio n to us that we need to impro ve o ur system! T ake yo ur t im e . Learning takes time. Rushing can have negative effects o n yo ur pro gress. Slo w do wn and let yo ur brain abso rb the new info rmatio n tho ro ughly. Taking yo ur time helps to maintain a relaxed, po sitive appro ach. It also gives yo u the chance to try new things and learn mo re than yo u o therwise wo uld if yo u blew thro ugh all o f the co ursewo rk to o quickly. Expe rim e nt . Wander fro m the path o ften and explo re the po ssibilities. We can't anticipate all o f yo ur questio ns and ideas, so it's up to yo u to experiment and create o n yo ur o wn. Yo ur instructo r will help if yo u go co mpletely o ff the rails. Acce pt guidance , but do n't de pe nd o n it . Try to so lve pro blems o n yo ur o wn. Go ing fro m

Acce pt guidance , but do n't de pe nd o n it . Try to so lve pro blems o n yo ur o wn. Go ing fro m misunderstanding to understanding is the best way to acquire a new skill. Part o f what yo u're learning is pro blem so lving. Of co urse, yo u can always co ntact yo ur instructo r fo r hints when yo u need them. Use all available re so urce s! In real-life pro blem-so lving, yo u aren't bo und by false limitatio ns; in OST co urses, yo u are free to use any reso urces at yo ur dispo sal to so lve pro blems yo u enco unter: the Internet, reference bo o ks, and o nline help are all fair game. Have f un! Relax, keep practicing, and do n't be afraid to make mistakes! Yo ur instructo r will keep yo u at it until yo u've mastered the skill. We want yo u to get that satisfied, "I'm so co o l! I did it!" feeling. And yo u'll have so me pro jects to sho w o ff when yo u're do ne.

Note

Understanding HTML is a prerequisite o f this XML co urse. If it has been a while since yo u've used HTML, o r yo u just need to brush up, check o ut o ur Intro ductio n to HTML and CSS co urse.

Lesson Format We'll try o ut lo ts o f examples in each lesso n. We'll have yo u write co de, lo o k at co de, and edit existing co de. The co de will be presented in bo xes that will indicate what needs to be do ne to the co de inside. Whenever yo u see white bo xes like the o ne belo w, yo u'll type the co ntents into the edito r windo w to try the example yo urself. The CODE TO TYPE bar o n to p o f the white bo x co ntains directio ns fo r yo u to fo llo w: CODE TO TYPE: White boxes like this contain code for you to try out (type into a file to run). If you have already written some of the code, new code for you to add looks like this. If we want you to remove existing code, the code to remove will look like this. We may also include instructive comments that you don't need to type. We may run pro grams and do so me o ther activities in a terminal sessio n in the o perating system o r o ther co mmandline enviro nment. These will be sho wn like this: INTERACTIVE SESSION: The plain black text that we present in these INTERACTIVE boxes is provided by the system (not for you to type). The commands we want you to type look lik e this.

Co de and info rmatio n presented in a gray OBSERVE bo x is fo r yo u to inspect and absorb. This info rmatio n is o ften co lo r-co ded, and fo llo wed by text explaining the co de in detail: OBSERVE: Gray "Observe" boxes like this contain information (usually code specifics) for you to observe. The paragraph(s) that fo llo w may pro vide additio n details o n inf o rm at io n that was highlighted in the Observe bo x. We'll also set especially pertinent info rmatio n apart in "No te" bo xes:

Note T ip

No tes pro vide info rmatio n that is useful, but no t abso lutely necessary fo r perfo rming the tasks at hand.

Tips pro vide info rmatio n that might help make the to o ls easier fo r yo u to use, such as sho rtcut keys.

WARNING

Warnings pro vide info rmatio n that can help prevent pro gram crashes and data lo ss.

Setting XML Mode Since Co deRunner is a multi-purpo se edito r, yo u'll need to use the co rrect Syntax. In this co urse, yo u will use XML fo r xml, xsl, and xsd files. To change to XML, select it fro m the dro p-do wn menu as sho wn:

Thro ugho ut the co urse, we will give yo u mo re tips fo r using the learning enviro nment effectively.

What is XML? XML is essentially an agreement amo ng peo ple to sto re and share textual data using standard metho ds. XML stands fo r eXtensible Markup Language. XSL stands fo r eXtensible Stylesheet Language. Data is sto red in XML do cuments. XSL do cuments describe ho w to change XML do cuments into o ther kinds o f do cuments (such as HTML, TXT o r even XML.) The pro cess o f transfo rmatio n is called XSLT, o r so metimes XSL transfo rmatio ns. Fo r example, suppo se I gave yo u the fo llo wing info rmatio n: Ale x Chilt o n 52 Arkansas Given just this info rmatio n, yo u wo uldn't kno w what it was fo r o r what it meant. XML pro vides a way to mark up this data so that it can be interpreted by o ther peo ple, as well as o ther co mputer pro grams. So , the data abo ve might be marked up like this: Ale x Chilt o n 5 2 Arkansas No w yo u kno w a lo t mo re abo ut the meaning o f this info rmatio n. After the XML tags are added, yo u can tell that Alex Chilto n is so me kind o f spo rts player, that his number is 52, and that he plays fo r the University o f Arkansas. It might lo o k a lo t like HTML to yo u, but the markup abo ve do esn't wo rk exactly like HTML. XML do esn't tell us ho w co ntent sho uld lo o k; instead, it gives it co ntext and meaning.

A History of XML and SGML XML is a pro duct o f the Generalized Markup Language (GML) that IBM created in the 19 6 0 s. GML was created to pro vide a standard way to mark up textual do cuments fo r po rtability acro ss different platfo rms. This pro ved to be a difficult task, but after nearly 20 years o f research, IBM's wo rk was ado pted by the Internatio nal Organizatio n fo r Standards (o r ISO—no pe, no t IOS, I do n't kno w why.) ISO ado pted IBM's GML and called it SGML, o r Standard Generalized Markup Language. SGML was the basis fo r co mplex do cumentatio n systems. Unfo rtunately SGML is a little too co mplex fo r general use (kinda funny, isn't it?).

HTML was created in the early 19 9 0 s as a subset o f SGML. It was designed to be a simple markup language to facilitate the transfer o f web-based do cuments. But HTML's simplicity can be limiting. Co nsider the different HTML that is so metimes required to display similar co ntent o n different bro wsers. In 19 9 6 , XML was created to reso lve the pro blems o f HTML and the co mplexities o f SGML. XML's strength was in its o rganizatio n. Do cument structure and presentatio n were finally separated, while the benefits o f standard markup pro cedure were maintained.

How is XML Being Used? O'Reilly Scho o l suppo rts the use o f XML, and all o f o ur co urses are written in XML. Separating the do cument structure fro m its presentatio n is impo rtant fo r us. Since o ur tuto rials are written in XML, o ur co urse develo pment staff do esn't have to wo rry abo ut fo rmatting co urse co ntent fo r the web, print, o r any o ther particular techno lo gy. The presentatio n is left up to design pro fessio nals. Our autho rs do n't have to spend time adding unnecessary tags to their do cuments. And design pro fessio nals do n't have to spend time changing multiple individual do cuments so they have a unifo rm appearance. If we decide to change the lo o k o f the HTML representatio n o f o ur tuto rials, we o nly have to edit one XSL file. Or, if we decide to make o ur tuto rials available in plain text fo rmat, we o nly have to create one XSL file. Go o d stuff! Other co mpanies have fo und po werful ways to use XML to o . The internet has demo nstrated that o pen techno lo gies and fo rmats are much mo re useful than pro prietary fo rmats. A file created o n a wo rd pro cesso r that saves do cuments in XML fo rmat can be read by ano ther wo rd pro cesso r that reads XML files. No co nversio n is necessary. That same file co uld also be o pened and mo dified by a database system. Finally, XML can be used as an intermediary between users. Fo r example, chemists can use XML to share info rmatio n with each o ther in a simple (yet po werful) fo rmat. Or banks can distribute financial info rmatio n to their custo mers in an XML fo rmat that all pro grams can read.

Parsing XML There are several techno lo gies available fo r parsing and transfo rming XML. C, C++, Perl, JavaScript, Pytho n, Java pro grams, Java servlets, PHP, and ASP can all wo rk with XML. The fo cus o f this co urse is not o n these o ther techno lo gies tho ugh, it's o n XML itself. Our co urse pro vides all o f the to o ls yo u'll need to use XML and XSL with o ur cho sen XML and XSL so ftware. Techno lo gies co me and go quickly, so we wo n't try to co mpare everything o n the market, but feel free to check o ut so me o f these great reso urces: xml.co m XML FAQ Cafe co n Leche XML News and Reso urces

A Basic XML Document No w that yo u kno w a bit abo ut the histo ry o f XML, let's see it in actio n!

WARNING

must be the first co de typed into yo ur XML file. No white space can be present befo re this text.

Be sure yo u're using XML syntax and type the co de belo w into Co deRunner as sho wn: CODE TO TYPE: This is my first XML document! When yo u finish, click the Che ck Synt ax butto n:

If yo u typed everything co rrectly, yo u will see No e rro rs f o und..

Co ngratulatio ns! Yo u just created yo ur first XML do cument! So , what do es this do cument do? By itself, this little example do esn't do anything. Remember, XML do cuments are intended to be read by co mputers and humans. Yo u co uld email this file to yo ur friend and she wo uld be able to understand it, o r yo u co uld write a pro gram in nearly any language (like Java o r Pytho n) to read the file and access its co ntents, witho ut a lo t o f messy pro gramming.

Differences Between XML and HT ML Since HTML is a markup language like XML, they have many similarities, but there are a few key differences between HTML and XML to o . Here are so me fundamental differences yo u sho uld be aware o f: With HTML, small erro rs in syntax are o ften igno red. HTML has o nly pre-defined tags, whereas XML tags are created by the autho r. Do cuments can be structured lo gically in XML (the autho r cho o ses the appro priate structure), while HTML has a pre-defined "head" and "bo dy" type structure. XML isn't always useful o n its o wn. Translating it to different fo rms (such as HTML) is o ne o f its great po wers. XML has different rules fro m HTML because XML was created to serve a different purpo se. Mo st o f the differences between HTML and XML syntax serve to make parsing XML do cuments faster and easier. No mistakes are allo wed in XML! These differences sho uld make sense to yo u. If they do n't, please take a lo o k at the HTML co urse again.

Common Mistakes Befo re we get to o deep into XML, yo u sho uld be aware o f so me co mmo n pitfalls o ut there waiting fo r new XML pro grammers. As an experienced HTML pro grammer, yo u're pro bably used to HTML's mo re flexible syntax structure. If yo u're co nverting existing HTML to XML (defining so me HTML tags in yo ur XML specificatio n), yo u may see so me pro blems.

White Space XML treats white space much differently fro m HTML. In HTML, white space (spaces, newlines, tabs, and o ther "white" characters) is pretty much igno red. This is no t the case in XML. Every character is impo rtant! Let me say that again. Every character is important! (Okay, no w that I've said that, depending o n yo ur applicatio n, it is po ssible that white space may no t matter. If yo u are using XML to o utput to HTML o nly, there is little need to wo rry abo ut this, but fo r strictly XML applicatio ns, it is impo rtant.)

Closing T ags All tags must be clo sed in XML. So a tag like
by itself is inco rrect in XML. To remedy this situatio n, yo u either use
o r

. These two metho ds o f clo sing tags are different. Yo u'll learn mo re abo ut that when we study DTDs later in the co urse.

Nesting T ags All XML tags must be nested co rrectly. In HTML, nesting is no t always impo rtant. Change yo ur XML so it lo o ks like this: CODE TO TYPE: This is some text. Check the syntax. This co de is inco rrect because the span tag is no t clo sed befo re the div tag. Structure is extremely impo rtant in XML (so impo rtant that yo ur do cument will no t be pro cessed if the structure is inco rrect!) Let's mo dify the co de and make it wo rk:

CODE TO TYPE: This is some text. Check the syntax again; it sho uld parse co rrectly no w.

Root Element In additio n to nesting elements co rrectly, XML requires a ro o t element, which co ntains all the o ther elements. Edit yo ur XML as sho wn: CODE TO TYPE: This is some text. This is red text. Check the syntax. This co de is inco rrect because there is no co ntaining element. Try adding ht m l tags as sho wn: CODE TO TYPE: This is some text. This is red text. Check the syntax again; it sho uld parse co rrectly no w.

Capitalization Ano ther drastic difference between HTML and XML is capitalizatio n. In HTML, bro wsers to lerate tags in upper o r lo wer case, o r even a co mbinatio n o f the two . This is no t the case with XML. Letter case is impo rtant in XML.

WARNING

XML is case-sensitive, so and are different!

Quoting Attributes Ano ther difference between HTML and XML may be in the syntax o f yo ur inline style attributes. Change yo ur XML as sho wn: CODE TO TYPE: This is some text. Check the syntax. Yo u may be used to typing a line this way, but it is inco rrect syntax in XML. The style attributes must have quo tatio n marks aro und them. Go ahead and fix the syntax. Mo dify yo ur co de as sho wn belo w: CODE TO TYPE: This is some text.

Check the syntax again. Okay, so no w that yo u're warmed up, let's keep go ing! Yo u're o n yo ur way to appreciating all the po wer that is XML. See yo u at the next lesso n! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Your First XML Document In this lesso n, we'll co nstruct a basic XML file fo r sto ring pho ne bo o k info rmatio n. Yo u might use this pho nebo o k to co ntain info rmatio n abo ut family and friends. Yo u co uld send yo ur pho nebo o k to yo ur family via e-mail, and they co uld add their o wn entries to the file. An XML file is a po werful fo rmat to use fo r yo ur pho nebo o k.

Phonebook XML The info rmatio n we want to sto re in this XML file is: First Name Last Name Pho ne Number Ho w can we implement this in XML? First, we have to define a set o f tags we want to use to keep track o f o ur info rmatio n. These tags are similar to the tags used in HTML because bo th HTML and XML are derived fro m SGML. But unlike HTML, XML has no predefined tags. Tags in XML are also referred to as elements. Fo r o ur example, we'll use these elements: Let's get started o n yo ur first pro gram! First, we'll create a fo lder to keep all o f o ur XML stuff o rganized. In the left panel o f yo ur Co deRunner windo w, right-click Ho m e , and select Ne w f o lde r... as sho wn:

Type xm l1 fo r the fo lder name, and press Ent e r:

No w let's create an XML file! Make sure yo u're in XML mo de in the edito r. This XML file will mark up info rmatio n that wo uld go into a pho nebo o k. Type everything exactly as it appears in the bo x here: CODE TO TYPE: Alex Chilton 1-800-123-4567

Click the Save butto n in the Co deRunner Edito r to o lbar, select the /xm l1 fo lder we just created, and type the filename Pho ne Bo o k.xm l. So what have we created? Even if yo u knew no thing abo ut XML, yo u might guess that this file co ntains a pho ne bo o k and o ne listing fo r Alex Chilto n, who so meho w go t himself an 8 0 0 number that is exceedingly easy to remember. The first line with indicates that this is an XML file. The seco nd line with specifies the document type o f the file we are creating (we decided this is a do cument o f type Pho neBo o k). Yo u will learn mo re abo ut DOCTYPE later, but fo r no w remember that yo ur DOCTYPE must match the first set o f tags in yo ur XML do cument. In this case, the !DOCTYPE is Pho neBo o k. is the first tag o f o ur do cument. The o ther lines in the XML do cument define yo ur pho nebo o k and the data it tracks. In this example, we are tracking First name, Last name, and Pho ne number. Ho w wo uld yo u add ano ther listing? Befo re we go any further, we'll want to include a few no tes. When yo u write XML (o r any o ther do cument o r pro gram) yo u sho uld co mment o ften. In this co urse, yo u'll always co mment yo ur files! Ultimately it saves time and impro ves the quality o f yo ur wo rk. Co mments in XML are similar to co mments in HTML, structured like (no te that no clo sing tag is needed in this case). Add a co mment as sho wn:

CODE TO TYPE: Alex Chilton 1-800-123-4567

Save it. We wo n't include the co mment in o ur fo llo wing examples in the interest o f brevity, but yo u sho uld use co mments liberally thro ugho ut yo ur co de. Remember: XML is case-sensitive. and are not the same! All XML tags must be clo sed. This means fo r every a matching must exist. is an example o f an single tag that is clo sed.

Note

must be the first thing in yo ur XML file. No white space can be present befo re this text.

No w let's set a style sheet fo r o ur XML file. Edit Pho ne Bo o k.xm l as sho wn: CODE TO TYPE: Alex Chilton 1-800-123-4567

Save it. This co de links o ur XML file to an XSL file we've pro vided. An XSL file is a style sheet fo r yo ur XML file. (In later lesso ns, we'll sho w yo u ho w to make XSL files o n yo ur o wn. Fo r no w we'll just do it fo r yo u to get where we want to go in this lesso n.) XSL is a language that describes a set o f transfo rmatio ns that are made o n a so urce XML file. We are using XSL to "preview" o ur XML as it might be represented in HTML. In o ther wo rds, this XSL file co ntains the rules fo r turning o ur XML file into HTML. Lo cate the Che ck Synt ax, Validat e , and T ranslat e butto ns in the to o lbar. To translate this page, click the T ranslat e butto n ( ). Yo u'll see yo ur pho ne bo o k file represented in HTML. Click o n the So urce tab in the po p-up windo w and yo u'll see the HTML co de. In a later lesso n, yo u'll learn ho w to change the HTML representatio n o f yo ur XML file.

Note

If yo u do n't see the HTML representatio n o f yo ur pho nebo o k, and/o r see an erro r message instead, there may be so mething wro ng with yo ur XML file. Check to make sure yo u typed it in co rrectly, witho ut spelling erro rs, and with all tags pro perly clo sed.

What do es the Translate butto n do ? It takes the so urce XML file and a set o f translatio n instructio ns (the XSL file) and o utputs the result. Here is a graphical representatio n o f the translatio n pro cess:

When yo u click T ranslat e , bo th files are sent to the OST XML Translator, which checks the files and then co mbines them to give HTML o utput. There are different parsers fo r different types o f o utput. In this intro ducto ry co urse, mo st o f o ur o utput will be HTML. Check the syntax and make no te o f the result. The message yo u see verifies whether o r no t yo ur do cument translated co rrectly and has the co rrect syntax. With this XML do cument wo rking, let's add ano ther listing. Mo dify Pho ne Bo o k.xm l as sho wn: CODE TO TYPE: Alex Chilton 1-800-123-4567 Laura Chilton 1-800-234-5678

Save it, then translate it and check o ut what happens. Yo u'll see two pho nebo o k entries—o ne fo r Alex Chilto n and o ne fo r Laura Chilto n. We will use this file as a starting po int in future lesso ns and o bjectives, so yo u'll want to save a co py. If yo u want to keep a separate co py o f the file as it is in this lesso n, use the Save As butto n ( ) and give it a different name. Be sure to include the xml extensio n when yo u're saving yo ur xml files. Then, reo pen the o riginal Pho ne Bo o k.xm l fro m the /xm l1 fo lder, and try adding mo re entries into yo ur pho nebo o k. No w let's change o ur XSL instructio ns so o ur resulting HTML do cument will lo o k different. Mo dify Pho ne Bo o k.xm l as sho wn:

CODE TO TYPE: Alex Chilton 1-800-123-4567 Laura Chilton 1-800-234-5678

and again. It's o ur pho nebo o k again, but it really lo o ks different no w! So o n yo u'll learn ho w to translate yo ur XML file into different representatio ns, then yo u'll really get a feel fo r the po wer o f XML! Save this file as Pho ne Bo o k0 2.xm l. Let's make so me mo re changes. Perhaps Alex Chilto n has three pho ne numbers—o ne fo r wo rk, o ne fo r ho me, and o ne fo r his cell pho ne. Ho w can we add mo re numbers to yo ur pho nebo o k in a way that makes sense? Type the co de belo w as sho wn to see o ne po ssible so lutio n: CODE TO TYPE: Alex Chilton 1-800-123-4567 1-555-222-3333 1-555-222-9999 Laura Chilton 1-800-234-5678

and

again. Yo u sho uld get so me interesting results! XML is o ne po werful language!

Of co urse, yo u'd want to kno w which o f these pho ne numbers was Alex's cell pho ne and which was his ho me pho ne to o . We'll get to that in just a bit. Fo r no w, let's make sure yo u understand ho w an XML file is structured.

T he T ree Structure of XML As yo u might have no ticed, XML do cuments fo rm kind o f a tree. Yo u're pro bably familiar with the way directo ries and files are o rganized o n yo ur co mputer—also in a tree-like structure. Here's o ur XML, represented by a tree with no clo sing tags:

(Well, kinda like a tree!) As yo u can see, "/" is the "ro o t"; fro m that springs the Pho neBo o k trunk, and fro m that, there are two Listings and each Listing has First, Last, and Pho ne branches. Inside First is the first name, inside Last is the last name, and inside each Pho ne is a pho ne number. In this example, Listing is a child element o f Pho neBo o k. Similarly, Pho neBo o k is the parent element o f Listing. First, Last, and Pho ne are all children o f Listing and grandchildren o f Pho neBo o k. Pho neBo o k is also the grandparent o f First, Last, and Pho ne. The termino lo gy we use to describe the relatio nships between elements in XML is the same as that used in genealo gy.

Elements and Attributes What is an Element? An element is a tag in o ur XML do cument, such as . Anything inside this particular tag sho uld be interpreted as the first name.

So me XML references use the wo rd node to refer to elements.

Note

The root element is the first element that o ccurs in an XML do cument. In this case, the Pho neBo o k element is the ro o t element.

Here is o ur basic pho nebo o k XML file: OBSERVE: Alex Chilton 1-800-123-4567 Laura Chilton 1-800-234-5678 Yo u can interpret the example abo ve like this: The XML Do cument is a Pho ne Bo o k (as is determined by the Pho neBo o k tag). All text between these tags sho uld be a part o f the pho nebo o k. The pho nebo o k co nsists o f a series o f zero o r mo re List ings. Each List ing has several co mpo nents, such as the First and Last names. Each List ing also has a Pho ne element, as yo u might guess, to specify the pho ne number. This do cument has a unique hierarchy. Ho w wo uld the do cument be changed if the elements were mixed up? Co mpare this example to the last o ne: OBSERVE: Chilton Alex 1-800-123-4567 Chilton Laura 1-800-234-5678 As it turns o ut, there is no significant difference between this do cument and the previo us o ne. Type it in, try it o ut, experiment! The HTML o utput will still lo o k the same. Try mixing up all o f the elements to see what happens. Mixing some elements will change the meaning o f yo ur XML do cument. The next do cument has the same tags as the previo us do cuments, but it has a different meaning.

OBSERVE: Chilton Alex 1-800-123-4567 Chilton Laura 1-800-234-5678 Ho w is this do cument different? Reading thro ugh the do cument wo uld suggest that the Listing has a Pho neBo o k element. The do cument also suggests the Pho neBo o k has elements o f Last, First, and Pho ne. This could be co rrect, but it isn't the intended "meaning" o f o ur pho ne bo o k. Okay, so ho w do es this do cument lo o k when we try to parse it in the same way as o ur o ther XML do cuments? Mo dify Pho ne Bo o k.xm l as sho wn: CODE TO TYPE: Chilton Alex 1-800-123-4567 Chilton Laura 1-800-234-5678

and No w

it. . The XML file yo u create might make perfect sense to yo u, but no t to the rest o f the wo rld.

Undo these last changes befo re pro ceeding! Yo u can use Ct rl+Z (o n PCs) o r Co m m and+Z (o n Macs) in Co deRunner to undo previo us changes, o ne at a time.

What is an Attribute? So metimes we need mo re detailed info rmatio n than an element can supply. We may want to mo dify o ur info rmatio n using attributes. Using attributes allo ws us to o rganize o ur info rmatio n lo gically, and keep o ur co de neat so it can be understo o d by o thers mo re easily. Because XML attributes mo dify elements, attributes canno t perfo rm their magic witho ut elements. Let's say we are wo rking with o ur XML-based pho ne bo o k. Many o f o ur friends have multiple pho ne numbers fo r multiple pho nes: o ffice, ho me, and cell. We could create new elements fo r each o f these pho nes, such as , , and so o n. This wo uld make it clear to anybo dy which data is specified by the tags. But Attributes are a mo re elegant so lutio n to the pro blem. Instead o f specifying a unique tag fo r the ho me, cell, and wo rk pho ne numbers, we'll specify this in the Pho ne tag itself. Make sure yo u specify the new stylesheet—Pho ne Bo o k3.xsl. Mo dify Pho ne Bo o k.xm l as sho wn:

CODE TO TYPE: Alex Chilton 1-800-123-4567 1-800-123-4568 1-800-123-4569 Chilton Laura 1-800-234-5678 So what do es this new XML do cument specify? It appears to be the same pho nebo o k we've been wo rking with all alo ng. The new part o f the do cument is the T ype attribute specified in the Pho ne element. and

it:

Pretty co o l, huh? Go fo rth and experiment by specifying o ther Pho ne Types. No w that yo u have a feel fo r using attributes, go ahead and create so me o f yo ur o wn. Change yo ur XML do cument to include mo re pho ne numbers, perhaps multiple o ffice numbers, fax numbers, scho o l numbers, whatever yo u want. Preview yo ur XML to see ho w this changes the HTML representatio n o f yo ur do cument. When yo u are previewing yo ur XML, keep in mind that no changes have been made to o ur XSL do cument specifying the transfo rmatio n fro m XML to HTML. Yo u'll learn ho w we acco mplish that sho rtly...

When T o Use Elements and Attributes We co uld try to define the specific times and places to use elements and attributes, but in XML pro gramming, as is o ften true in life, there is no single co rrect fo rmula. It's a given that yo ur do cument sho uld include at least

as is o ften true in life, there is no single co rrect fo rmula. It's a given that yo ur do cument sho uld include at least o ne element, o r yo ur do cument wo uld be empty. Yo u co uld have yo ur who le XML do cument co nsist o f a single element with multiple attributes, but this wo uld be pretty messy and difficult to read. Instead. a balance is required. It's been o ur experience that using elements with attributes interspersed sparingly is the best way to create XML. The mo st impo rtant co nsideratio n is readability. Design yo ur XML do cuments so they can be read by anybo dy—even tho se unfo rtunates who haven't taken this co urse—and so readers can fo llo w the hierarchy o f yo ur do cuments. If yo u canno t give yo ur XML do cument to so mebo dy else and have them identify its co ntent (o r at least o ffer a go o d guess), then yo u pro bably need to rethink yo ur cho ices.

Finding and Fixing Syntax Errors In keeping with OST's philo so phy o f learning by do ing, mangling, fixing, and getting plain dirty with pro grams, I ask yo u no w to go ahead and break the Pho neBo o k XML file. Of co urse yo u have misgivings abo ut thrashing yo ur o wn beautiful co de, so click the New File butto n (

) and try o ut this pre-bro ken XML file:

CODE TO TYPE: Alex Chilton 1-800-123-4567 Laura Chilton 1-800-234-5678 Click to see what's wro ng with the file. Yo u'll no tice that it is ho rribly bro ken. This might happen if yo u're typing an XML file quickly. A sample erro r message may lo o k like this (o nly witho ut the co lo r):

OBSERVE: Your document has a syntax error. Fatal Error: Opening and ending tag mismatch: Phone line 9 and Listing on line 10 colum n 15 Fatal Error: Opening and ending tag mismatch: First line 12 and Phone on line 14 column 16 Fatal Error: Opening and ending tag mismatch: First line 12 and Phone on line 14 column 38 Line 1 2 3 4 5 6 Alex 7 Chilton 8 9 1-800-123-4567 10 11 . . . This means that yo ur XML file has an erro r. The pro cesso r fo und the first erro r at line s 9 and 10 . The erro r fo und at that line is Ope ning and e nding t ag m ism at ch. Also listed is yo ur do cument, with the line highlighted where the erro r o ccurs. In this case it is . To co rrect this pro blem, go to line 9 o f yo ur XML file. Yo u'll no tice that we never clo sed yo ur Pho ne tag o n the previo us line. Go ahead and fix that.

Note

The erro r messages generated by each XML/XSLT pro gram will be different, but they sho uld all give yo u a line number and erro r message.

Yo u may also see this message if yo u try to use characters such as less-than < o r greater-than > in yo ur element co ntent. Since these characters are used to define tags in XML, yo u really can't use them in the bo dy o f yo ur XML do cument witho ut so me special co nsideratio n. A po ssible way aro und this will be discussed in a future lesso n.

XML Vocabulary Befo re yo u go any further in this co urse, here's so me vo cabulary co mmo n to XML and XSL that yo u need to kno w: XML

eXtensible Markup Language

XSL

eXtensible Stylesheet Language

DTD

Do cument T ype Definitio n. A set o f rules that specifies the structure o f XML do cuments. DTDs are no t XML do cuments. Instead, DTDs have a different structure. Yo u'll see mo re abo ut DTDs in the next lesso n.

XSD, o r Like a DTD, a set o f rules that specifies the structure o f XML do cuments. A schema is an XML do cument Schema itself. Yo u'll learn mo re abo ut schemas in future lesso ns. Element

A tag in o ur XML do cument. An example o f an HTML element is , where the text in gre e n is an element.

Attribute

Additio nal values added to elements. An example o f an HTML attribute (with element) is , where the text in re d is the attribute.

Entities

A metho d o f search and replace. In XML, entities all begin with an ampersand (&) and end with a semico lo n (;). Yo u define yo ur entity and the text that is replaced by it. If yo u define an entity fo r &co m pany; with the text "Aweso meCo rpUSA," o ccurrences o f &co m pany; in yo ur XML do cument will be replaced with "Aweso meCo rpUSA." We'll talk abo ut this in greater detail in a future lesso n.

XML T ools One o f the best qualities o f XML is that it do esn't require any to o ls fo r humans to begin using it. XML itself is readable and understandable by humans. Editing XML do esn't require anything mo re than a simple text edito r. This isn't ho w yo u'll ultimately use XML, but fo r no w it's a go o d way to practice. Pro gramming applicatio ns that use XML are beyo nd the sco pe o f this co urse, but understanding the co ncepts that lead up to XML applicatio ns is impo rtant. In this co urse we use Co deRunner as o ur text edito r. Che ck Synt ax, Validat e , and T ranslat e are three impo rtant to o ls that are available fo r us to use with XML in Co deRunner.

Non-Validating Parsers In Co deRunner, Che ck Synt ax takes yo ur XML do cument and runs it thro ugh an XML parser in a no nvalidating mo de. This means that yo ur XML is no t checked against any DTD o r schema. (A DTD is o ne way to specify a set o f rules yo ur XML do cument must fo llo w; a schema is ano ther. Yo u'll learn mo re abo ut tho se later.) As lo ng as yo ur XML is well-formed, it will parse co rrectly. Ho w might this be useful? If yo u are abso lutely sure that yo ur XML file is structured co rrectly, yo ur applicatio n may execute faster if it do esn't have to check yo ur file against its DTD o r schema.

Validating Parsers In Co deRunner, Validat e takes yo ur XML do cument and runs it thro ugh an XML parser in validating mo de. Yo ur XML is checked against its DTD o r schema. Yo ur do cument must be well-fo rmed and valid fo r it to parse co rrectly. If yo u are no t already certain that yo ur XML files are structured co rrectly, yo u can use Validate to check them to make sure that they are.

T ranslating In Co deRunner, T ranslat e takes yo ur XML do cument and an XSL do cument and runs them thro ugh an XSLT engine. The XSLT engines require two inputs: o ne XML do cument and o ne XSL do cument. XSLT engines pro vide o ne o utput: the result o f the XSL do cument's translatio ns applied to the XML do cument. XSLT engines must use an XML parser to read the XML and XSL do cuments. Co deRunner perfo rms a no nvalidating parse o f yo ur XML do cument and the XSL do cument when yo u Translate. XML parsers and translato rs are used in vario us ways. If yo u're kno wledgeable in a language such as Java, yo u may kno w ho w to create yo ur o wn "Perso nal Info rmatio n Manager" pro gram to keep yo ur address bo o k—sto red as an XML file. As lo ng as yo u kno w ho w to write a text file in Java, yo u can sto re yo ur data as XML. Reading these XML files back in, tho ugh, is not easy. Instead o f spending time and energy pro gramming a way to read XML files, why no t use a pre-written Java parser? Many Java parsers have been written specifically to be included in Java pro grams. No w yo u can use the functio ns that the parser pro vides to read yo ur XML file. Pro blem so lved! Similarly, if yo u want to add XSLT functio nality to yo ur pro gram, there are many engines that have been written specifically to be included in o ther applicatio ns. Ano ther pro blem so lved! We are all abo ut so lving pro blems and saving time. And fo r this co urse, yo u sho uld also save yo ur wo rk, because yo u will co ntinue to use yo ur Pho neBo o k file thro ugho ut the co ming lesso ns. See yo u in the next o ne! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Using DTDs (Document Type Definitions) What is a Document T ype Definition? A Do cument T ype Definitio n, o r DT D, is a set o f rules yo u define fo r yo ur XML do cuments. Why wo uld this be impo rtant, yo u ask? Well, let's say yo u're using XML to exchange financial info rmatio n between yo ur bank and yo ur ho me co mputer. If the fo rmat o f yo ur XML do cument isn't co nsistent thro ugho ut, yo ur savings acco unt data co uld be interpreted as yo ur checking acco unt data, causing all kinds o f co nfusio n. DTD enables us to define the co mpo nents o f an XML do cument and put them where we want them to be. But wait, there's mo re! DTDs can also be used to define entities, o r co mmo n elements fo r XML do cuments. Here's what DTDs do : Define all elements and attributes o f an XML do cument. Define the o rder in which elements and attributes can o ccur. Define all entities that can be used in an XML do cument. Define the do cument type.

Declaring Use of a DT D in Your XML file DTDs can be sto red in yo ur XML do cument o r in a separate file. If yo ur DTD is go ing to be lo cated inside yo ur XML file, it must be at the top o f yo ur file (after the declaratio n). The beginning o f an internal DTD lo o ks like this: OBSERVE: Mo re likely, yo u will sto re yo ur DTD o utside yo ur XML do cument. The beginning o f an XML file that has an external DTD lo o ks like this: OBSERVE: Let's break do wn this co de: m ydo ct ype is the do cument type. If yo ur XML do cument will ho ld perso nal banking info rmatio n, yo ur do ctype might be f inancial o r banking. SYST EM is used if yo ur DTD is a separate file fro m yo ur XML do cument. Yo u can define yo ur DTDs to be either PUBLIC o r SYST EM, depending o n yo ur applicatio n. PUBLIC means that all are free to use the DTD. This may be the case if yo ur DTD is defining the XML standard fo r financial exchange between banks. If yo u are using yo ur o wn DTD, yo u'll use SYSTEM. (Fo r this co urse, yo ur DTDs will be internal, so yo u wo n't have to wo rry abo ut this.) ht t p://www.m ysit e .co m /dt ds/m ydo ct ype .dt d indicates the lo catio n (o r URL) o f the DTD file if yo ur DTD is a separate file. Here's an example o f an XML do cument with an internal DTD:

OBSERVE: ]> Alex Chilton 1-800-123-4567 Laura Chilton 1-800-234-5678 Here's an example o f an external DTD: OBSERVE:
PhoneBook (Listing+)> Listing (First,Last,Phone+)> First (#PCDATA)> Last (#PCDATA)> Phone (#PCDATA)> Phone CDATA #REQUIRED>

External DTDs usually have a .dt d extensio n. Yo u can save them anywhere as lo ng as yo u specify the pro per lo catio n o f yo ur DTD in yo ur XML file.

Internal, External, or Neither? So , which type o f DTD sho uld yo u use? Well, that really depends o n yo u and yo ur needs. Use the DTD type that's mo re co nvenient fo r yo u and makes yo ur XML do cument mo re clear to read and easier to understand. Fo r instance, if yo ur XML do cument will be used frequently as a standard fo rmat fo r a series o f textbo o ks, why no t include a pre-defined DTD? If yo ur XML do cument is unique and basic, like here in yo ur perso nal pho nebo o k, yo u may want to keep it simple and include the DTD with yo ur XML do cument. On o ccasio n yo u might find yo urself using a no n-validating XML pro cesso r. This means that the DTD fo r yo ur XML do cument will be igno red. The XML pro cesso r will check yo ur XML do cument to make sure it fo llo ws the rules o f XML, but it won't check to see if it fo llo ws the DTD.

DT D: T he Specifics Okay, let's try an example! We will specify an internal DTD fo r o ur pho ne bo o k file. Elements o f the DTD are the same as XML elements. An element declaratio n in XML lo o ks like this: OBSERVE:

What do es this declaratio n mean? First is the name o f the element that will be in the XML do cument. (# PCDAT A) specifies that the co ntent o f the First element will be PCDATA, o r parsed character data, which is text that will be parsed by an XML parser. (CDATA, o r character data, is text that will no t be parsed by an XML parser.) Yo u can specify as many elements as yo u need. So metimes yo u may want to specify the o rder in which elements will be accessed as well. Using the pho ne bo o k XML do cument, we may want to specify that the First name appears befo re the Last name. The DTD wo uld lo o k like this: OBSERVE: No w in o ur List ing tag, two elements must appear: First and Last (in that o rder). No w, mo dify yo ur Pho ne Bo o k.xm l file as sho wn: CODE TO TYPE: [ ]> Alex Chilton 1-800-123-4567 1-800-123-4568 1-800-123-4569 Laura Chilton 1-800-234-5678

and

it. Yo u'll see this text:

OBSERVE: Error: Element Phone does not carry attribute Type on line 22 What do es this mean? Yo ur XML do cument was co mpared to its internal DTD (the very same o ne we just created). The DTD specifies that the element Pho ne must have an attribute o f T ype , always. A Pho ne element in o ur XML file do esn't have a T ype attribute, so o ur XML parser gave us an erro r. Go ahead and add the T ype attribute and set it to "ho me." Validate the do cument o nce again; that erro r will go away. No w try remo ving the Pho ne elements co mpletely. Take o ut the co de in red as sho wn:

CODE TO TYPE: ]> Alex Chilton 1-800-123-4567 1-800-123-4568 1-800-123-4569 Laura Chilton 1-800-234-5678

and

it again. Yo u'll get this erro r:

OBSERVE: Error: Element Listing content does not follow the DTD, expecting (First , Last , Phone +), got (First Last ) on line 12 This erro r means that the DTD requires at least o ne Pho ne element to o ccur after the Last element. Since no ne exist, an erro r is raised. Resto re the deleted pho ne numbers to co rrect the erro r. Experiment with this co ncept fo r a while. Change yo ur DTD and XML file and deliberately break the validatio n. No te the erro rs that are generated. Belo w yo u will find mo re info rmatio n o n the syntax o f DTDs. Try them all! Sym bo l

De script io n

Exam ple

+

The element must o ccur o ne o r mo re times.



()

Defines a set o f elements. Used with o ther mo difiers.



,

Defines the o rder that the elements must o ccur.



|

Defines a set o f elements, o f which o ne must be used.

?

The element can o ccur zero o r o ne times.



*

The element (o r set) must o ccur zero o r mo re times.



Entities So metimes yo u'll find yo urself using the same text o ver and o ver again in yo ur do cuments. Yo u might want to include yo ur name, co mpany name, o r a co pyright no tice in every do cument. Fo rtunately, yo u do n't have to cut and paste this text o ver and o ver—just declare yo ur o wn entity in yo ur DTD: OBSERVE:

This entity declaratio n states that Co m pany co ntains "O'Reilly Media." If yo u are familiar with C o r C++, this is similar to the #DEFINE syntax. Once defined, yo ur entity will be available like any o ther system entity. To use yo ur declared entity, include the entity reference preceded by an ampersand (&) and fo llo wed by a semi-co lo n: OBSERVE: &Company; Try it in yo ur do cument. Mo dify Pho ne Bo o k.xm l as sho wn: CODE TO TYPE: ]> Alex Chilton 1-800-123-4567 1-800-123-4568 1-800-123-4569 &Company; Laura Chilton 1-800-234-5678

and

it. If yo u typed everything co rrectly, yo u'll see this:

&Co m pany; was replaced with O'Re illy Me dia There are five entities predefined in XML. All XML pro cesso rs are required to suppo rt these entities: Ent it y nam e Re place m e nt t e xt lt

The less than sign ()

amp

The ampersand (&)

apo s

The single quo te o r apo stro phe (')

quo t

The do uble quo te (")

ANY and EMPT Y So metimes yo u'll find that yo ur element sho uld be empty. Other times yo u may no t care if elements are co ntained within o ther elements (kind o f like the in HTML). Yo u can define these elements in yo ur DTD. Check this o ut: OBSERVE: In this example, ANY means that any o ther element can o ccur in between and , and EMPT Y can be used to deno te when an element must be empty. Let's try this o ut. Edit Pho ne Bo o k.xm l as sho wn:

CODE TO TYPE: ]> Alex Chilton 1-800-123-4567 1-800-123-4568 1-800-123-4569 yes &Company; Laura Chilton 1-800-234-5678 This do cument has a DTD that suppo rts a new element: Marke d. The element is allo wed after the Pho ne element, and must be empty.

and

it. Yo u'll get an erro r like:

OBSERVE: Error: Element Marked was declared EMPTY this one has content on line 21 This is a lo ng way o f saying that the element must be empty. Yo u can fix the pro blem by remo ving the "yes" as we did with Laura: OBSERVE: or OBSERVE: yes

What is a Content Model? No w that yo u've learned the basics o f DTD co nstructio n and the right way to define elements, attributes, and entities, yo u can use them to define yo ur Content Model. A co ntent mo del pro vides the framewo rk fo r data to flo w into , as it is entered into yo ur XML do cument, thus keeping the data o rganized and co rrectly fo rmatted. Suppo se yo u are creating an XML do cument to sto re co ntact info rmatio n fo r yo ur friends. Yo u want to include bo th ho me and wo rk addresses as part o f this info rmatio n. Yo u co uld use this:

OBSERVE: 123 Sesame Street, Madison WI 53704 236 Main Street, Madison WI 53704 Or yo u co uld use this: OBSERVE: 123 Sesame Street Madison WI 53704 236 Main Street Madison WI 53704 Thro ugh careful definitio n o f yo ur elements and attributes, yo u cause the XML do cuments that use yo ur DTD to have a specific structure. If yo u create an XML do cument that do esn't fo llo w the rules yo u've specified in yo ur DTD, the XML applicatio n yo u are using at the time will refuse to wo rk. This is impo rtant in situatio ns like o ur o nline banking example. Yo u want yo ur ho me financial so ftware to accept XML data fo r all o f yo ur transactio ns, and yo u want to make sure the data is fo rmatted co rrectly. If yo ur so ftware checks the inco ming XML data against its DTD, inco rrectly fo rmatted XML files will no t be pro cessed. This is ho w it sho uld be— yo u do n't want an inco rrectly fo rmatted XML do cument to mess up all o f yo ur financial info rmatio n! Alright, yo u're lo o king go o d so far. Keep it up and see yo u in the next lesso n! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Introduction to Schemas What is a Schema? In the last lesso n we lo o ked at DTDs, and ho w a DTD co uld be used to specify the elements and attributes that can o ccur in an XML do cument. These rules determine whether an XML do cument is valid. Schemas also define a set o f rules fo r XML do cuments, but are much mo re po werful than DTDs. DTDs can be included within an XML file o r they can be external. Schemas are always external files. Schemas: are written in XML, so existing XML to o ls can be used to determine whether a schema is valid. suppo rt data types, so yo u can be sure that data types in yo ur XML file are handled pro perly. are extensible, so they can handle future enhancements gracefully. If schemas are so great, why do we need DTDs? Great questio n! As a casual XML user yo u may no t need a schema to define the rules fo r yo ur XML file. A simple internal DTD may be sufficient. We are nearly ready to create o ur o wn schema, but befo re we do , we need to discuss nam e space s.

What is a Namespace? Namespaces are used in XML files to avo id naming co llisio ns. Suppo se yo u want to co mbine yo ur pho nebo o k XML file with yo ur friend's, but yo ur pho nebo o k lo o ks like this: OBSERVE: 312-555-1212 Meanwhile, yo ur friend's pho nebo o k XML file has elements that co ntain o ther elements, like this: OBSERVE: home 312-555-1212 Yo u can merge these two representatio ns by using namespaces. Type the co de belo w into a new XML file as sho wn: CODE TO TYPE: 312-555-1212 home 312-555-1212

Save it in yo ur /xm l1 fo lder as Addre ssBo o k.xm l.

OBSERVE: 312-555-1212 home 312-555-1212 This do cument has two namespaces with two prefixes: m y and o t he r. This do cument isn't exactly right, tho ugh, because we haven't specified the namespaces allo wed in o ur do cument. and no te the erro rs: OBSERVE: Error: Error: Error: Error:

Namespace Namespace Namespace Namespace

prefix prefix prefix prefix

my on other other other

Phone is not defined on line 3 column 15 on Phone is not defined on line 6 column 13 on Type is not defined on line 7 column 16 on Number is not defined on line 8 column 18

To specify the namespaces allo wed in o ur do cument, we need to mo dify o ur XML file's ro o t element—in this case, Co m bine dPho ne Bo o k. Change the do cument as sho wn belo w: CODE TO TYPE: 312-555-1212 home 312-555-1212

Save it and

. No erro rs! Let's take a clo ser lo o k at the new co de:

OBSERVE: xmlns:my="http://oreillyschool.com/myPhoneBook/" xmlns:other="http://oreillyschool.com/otherPhoneBook/" Here we used xm lns to define the m yPho ne Bo o k and o t he rPho ne Bo o k namespaces, with URLs o f ht t p://o re illyscho o l.co m /m yPho ne Bo o k/ and ht t p://o re illyscho o l.co m /o t he rPho ne Bo o k/. The URLs uniquely identify the namespace, and are no t actually used by XML parsers. Instead, they are o ften used to direct humans to a web page describing the namespace. This next co ncept is impo rtant, so let me drive it ho me: m y is the prefix we are using fo r the ht t p://o re illyscho o l.co m /m yPho ne Bo o k/ namespace o t he r is the prefix we are using fo r the ht t p://o re illyscho o l.co m /o t he rPho ne Bo o k/ namespace. If o ur XML do cument o nly co ntains elements and attributes fro m a single namespace, we can fo rget abo ut the

prefixes and still use a namespace by declaring a default namespace: OBSERVE: 312-555-1212 This do cument still uses the same namespace as befo re (the myPhoneBook namespace), but we have o mitted the prefix fro m the bo dy o f the do cument. Schemas are just ano ther XML do cument, but they are written using a specific namespace, as yo u will see sho rtly.

Your First Schema Our little pho nebo o k fro m the last lesso n is fine fo r sto ring a limited amo unt o f perso nal info rmatio n fo r a limited number o f co ntacts. But we can co me up with a better structure that will let us sto re even mo re details abo ut o ur friends, family, and business co ntacts. Mo st peo ple have multiple pho ne numbers, addresses, email addresses, instant message user names, and websites. So metimes this info rmatio n can be gro uped into "ho me" and "wo rk" catego ries. It wo uld also be handy to sto re o ther details abo ut o ur co ntacts such as birthdays, names o f family members, and even rando m no tes like "Alex is allergic to shellfish." We'll sto re this info rmatio n in an address bo o k, which will eventually lo o k so mething like this: OBSERVE: Alex M. Chilton 1950-12-28 123 4th Street Suite 505 San Fransisco CA 94101 US 555-1212 achil [email protected] http://bigcorp.com.us/ 555-9152 achil101 We'll start with a straightfo rward and basic file. Clear yo ur Addre ssBo o k.xm l file, and type in the co de belo w as sho wn:

CODE TO TYPE: In this new XML file, we've defined the namespace used by schemas. The text prefix o f the namespace we are including within o ur do cument is xsi. The file name o f the XML schema itself is specified by the xsi:no Nam e space Sche m aLo cat io n attribute. The file name o f o ur schema will be Addre ssBo o k.xsd.

Note

Make sure yo u type file names exactly as yo u see them in the lesso n. Case is impo rtant. If yo u save yo ur files in different directo ries, yo u need to update yo ur file names acco rdingly.

Check the syntax. If everything is o kay, yo u'll see No e rro rs f o und. Schemas are a type o f XML file, so we'll co ntinue to use the XML syntax in Co deRunner to autho r o ur file. Click Ne w File (

), and type the co de belo w as sho wn:

CODE TO TYPE:

Save it in yo ur /xm l1 fo lder as Addre ssBo o k.xsd. Here we used the xs:e le m e nt element to define o ne allo wable element in o ur XML file: an element named Addre ssBo o k. Check the syntax. If everything is o kay, yo u will see No e rro rs f o und. Next, switch back to yo ur Addre ssBo o k.xm l file, and click co rrect, yo u'll see the fo llo wing:

. If everything in yo ur XML file and schema is

Co ngratulatio ns! Yo u have created yo ur first schema! No w let's specify which child elements that AddressBo o k can co ntain. Add the co de belo w to yo ur AddressBo o k.xsd schema as sho wn: CODE TO TYPE:

Save it. We just added a co m ple xT ype element, which marks AddressBo o k as a complex element. Co mplex elements can: be empty. co ntain o nly elements. co ntain o nly text. co ntain elements and text.

Note

We'll discuss co mplex types and o ther data types in great detail in the next lesso n.

The next step is to define the o rder in which we want child elements to o ccur using the xs:se que nce element. AddressBo o k will have o nly o ne allo wable child element named Co nt act , but that child element can o ccur as many times as needed. Add the fo llo wing co de to yo ur schema:

CODE TO TYPE:

Save it. With o rdering in place, we can no w set Co ntact as the child element o f AddressBo o k. To do this, we'll use the xs:e le m e nt element, alo ng with so me new bits. Add the co de belo w to yo ur schema: CODE TO TYPE:

Save it. Let's take a clo ser lo o k: OBSERVE: In this co de, we specify that o ur pho nebo o k do esn't need Co nt act elements using m inOccurs=" 0 " , and that it can have as many co ntacts as we want by using m axOccurs=" unbo unde d" . Save yo ur schema again because it's time to take it fo r a test drive! Switch back to yo ur xml file and click f o und. Excellent!

. If yo ur schema and XML files are co rrect, yo u'll see No e rro rs

No w try adding a few Co ntact elements to yo ur Addre ssBo o k.xm l file. Type the co de belo w as sho wn: CODE TO TYPE: Alex Chilton Laura Chilton

and

. If yo ur schema and XML files are co rrect, yo u'll see No e rro rs f o und.

Let's intro duce an erro r to the XML file, just to make sure the schema is wo rking co rrectly. Change the first Co ntact element to a Pho ne element, as sho wn: CODE TO TYPE: 555-1212 Laura Chilton

and

. Lo o ks like o ur schema caught this erro r:

Experiment with the XML do cument—try adding o ther elements o r attributes to see what so rt o f erro r messages are returned. Remember the schema is no t co mplete yet, so no t everything will return an erro r. When yo u finish experimenting, resto re yo ur Addre ssBo o k.xm l to the last go o d versio n. Excellent wo rk so far! In this lesso n we've o nly to uched o n the basics o f schemas, but in the next lesso n, we'll dive deeper into schemas and lo o k at data types. See yo u there! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Data Types Welco me back! In the last lesso n, we had o ur first experience wo rking with schemas. In this lesso n, we will co ntinue to add features to o ur Address Bo o k schema.

Data T ypes In the pro gramming wo rld, data types are like rules fo r allo wed values. Here are so me examples o f data types that are co mmo n in many different pro gramming languages: Dat a T ype

integer

decimal string

Sam ple Value s -1 0 2 20 19 5 -19 50 39 10 .59 0 .0 0 -25.9 5 This is a sample string. #2 sample

Data types are impo rtant. Yo u wo uld be co nfused if yo u lo gged into yo ur bank's web site and fo und yo ur acco unt balance to be a string like O'Re illy Me dia as o ppo sed to a value like $ 35 9 0 .85 . The XML Schema definitio n describes several data types. Like XML itself, data types fo rm a hierarchy. At the ro o t o f the hierarchy is anyType, which literally means "any type." Unless yo u specify o therwise, the data type fo r elements and attributes is assumed to be anyT ype . The hierarchy lo o ks like this:

Fo r mo re info rmatio n o n built-in data types, visit w3.o rg. There are two children o f anyType: co mplex types, and anySimpleType. In XML, simple types canno t have attributes o r child elements; co mplex types can. Take a lo o k at the integer data type abo ve. It has additio nal specific child types, such as nonPositiveInteger and positiveInteger. Types can be extended to be mo re specific (fo r example, po sitive integers between 5 and 10 ). We'll wo rk o n this in a future lesso n. There are many different simple types defined—including lo ts o f different types o f integers, strings, and dates. Here are a few o f the mo re impo rtant o nes: Dat a T ype

Sam ple Value s -1

0 2 20 19 5 -19 50 39

xs:integer

10 .59 0 .0 0 -25.9 5

xs:decimal xs:string

This is a sample string. #2 sample

xs:date

20 0 0 -0 1-0 1

We will use bo th co mplex and simple types to expand o ur Address Bo o k schema.

Complex Data T ypes Befo re we get back to o ur schema, let's take ano ther lo o k at the address bo o k we're building: OBSERVE: Alex M. Chilton 1950-12-28 123 4th Street Suite 505 San Fransisco CA 94101 US 555-1212 achil [email protected] http://bigcorp.com.us/ 555-9152 achil101 Our schema isn't finished yet, so o ur address bo o k isn't quite that big. Open Addre ssBo o k.xm l fro m the last lesso n. It sho uld lo o k like this:

OBSERVE: Alex Chilton Laura Chilton Next, o pen the Addre ssBo o k.xsd file fro m the last lesso n. The schema sho uld lo o k like this: OBSERVE: Currently, o ur AddressBo o k element has a definitio n fo r o ne data type—an unnamed co mplex type that specifies o ne element named Co ntact. In the last lesso n, we learned that it's po ssible fo r co mplex elements to : be empty. co ntain o nly elements. co ntain o nly text. co ntain elements and text. Then we used a se que nce element as an order indicato r. Po ssible o rder indicato rs are: se que nce : child elements must o ccur in a specific o rder, and can o ccur as few o r as many times as necessary. all: child elements can appear in any o rder, but each element can o nly o ccur o nce. cho ice : o ne child element o r ano ther can o ccur. Co ntact can o ccur as many times as we want it to o r no t at all, because we set m inOccurs to 0 and m axOccurs to unbo unded (in this co ntext, "unbo unded" means "as many times as we need.") m inOccurs and m axOccurs are kno wn as occurrence indicato rs, because they specify the exact number o f times an element can o ccur in a do cument. This unnamed co mplex type is specific to the AddressBo o k element, and canno t be referenced o r used anywhere else. As an alternative to this setup, we co uld define an "Address Bo o k" co mplex data type elsewhere in o ur XML file. Change yo ur Addre ssBo o k.xsd schema as sho wn:

CODE TO TYPE: Here we defined a new data type named Addre ssBo o kT ype . We'll make o ne mo re change to o ur schema; we need to specify this data type fo r the AddressBo o k element. We can also sho rten the xs:e le m e nt : CODE TO TYPE: Validate yo ur XML do cument against this updated schema. If yo u typed everything co rrectly, yo u'll see No e rro rs f o und. No w that we've defined AddressBo o kType, we'll define Co ntactType to suppo rt these elements: FirstName MiddleName LastName Birthday There are several restrictio ns we need to place o n this data. Co ntacts must be allo wed o nly o ne first name, middle name, last name, and birthday. Birthdays sho uld also be a valid date, as o ppo sed to values like "Yes" o r "Mo nday." Let's start making changes! Edit yo ur schema as sho wn belo w:

CODE TO TYPE: This change sets the data type o n the Co ntact element to be Co ntactType. We still need to define this co mplex type. Remember, Co ntactType is a co mplex data type because it will o nly co ntain o ther elements. So we'll use xs:co mplexType and xs:sequence in o ur schema. Add the co de belo w to yo ur schema as sho wn: CODE TO TYPE: Next, we'll define the first fo ur elements allo wed inside o f Co ntact, with appro priate rules to specify ho w many times each element can o ccur. Edit yo ur schema as sho wn belo w:

CODE TO TYPE:

No w we're getting so mewhere! Let's test this schema. Save yo ur schema, then switch back to yo ur Addre ssBo o k.xm l file, remo ve the existing names, and add a first and last name, as sho wn belo w: CODE TO TYPE: Alex Chilton Laura Chilton Alex Chilton Validate yo ur XML. Yo u'll see No e rro rs f o und. What happens if yo u change the o rder o f the elements inside o f Co ntact? Try it! Mo dify Addre ssBo o k.xm l as sho wn belo w: CODE TO TYPE: Chilton Alex Chilton Validate yo ur XML. It lo o ks like the file has erro rs!

Order is impo rtant in o ur XML do cument because we specified a se que nce in the schema: OBSERVE: . . .

. . . Elements inside o f Co nt act must o ccur in the fo llo wing o rder: First Nam e , Middle Nam e , Last Nam e , and Birt hday. Middle Nam e and Birt hday do no t have to o ccur at all, but First Nam e and Last Nam e must o ccur. All elements can o nly o ccur o nce. No w delete the Last Nam e fro m yo ur XML do cument and Validat e . Yo u'll see an erro r like this:

Add the Last Nam e back after First Nam e to make this erro r go away.

Simple Data T ypes To co mplete the definitio n o f the elements in o ur schema, we need to specify the simple data type (o r allo wed values) fo r each element in o ur Co nt act T ype . Simple data types canno t have attributes o r child elements. This wo rks well fo r elements like First Nam e and Birt hday—we do n't want tho se elements to have children o r any attributes. In the chart at the beginning o f this lesso n, we saw a few simple data types that schemas can use. Here are a few: Dat a T ype Sche m a T ype

Sam ple Value s

Integer

xs:integer

5, 10 , -50 o r 29 0 59 259

Decimal

xs:decimal

5.29 , 10 .9 0 , -50 .0 0 0 0 0 1 o r 29 0 59 259 .6

String

xs:string

Any character, including punctuatio n and spaces!

Date

xs:date

20 10 -0 5-0 1

Let's start by picking an appro priate string data type fo r the name elements.

String T ypes Strings o ften co ntain white space: spaces, empty lines, and tabs. As yo u might remember fro m an earlier lesso n, white space in XML is important. Co nsider this sho rt example o f XML: OBSERVE: John Smith 150 N. Michigan Suite 500 Chicago, IL 60601 When an XML pro gram reads this element, it might see this:

OBSERVE: \n\tJohn Smith\n\n150 N. Michigan\nSuite 500\nChicago, IL 60601\n In o ur example, new lines were replaced by the special sequence \n and tabs were replaced by \t . Yo u may no t have no ticed this befo re, but the beginning and the end o f the name and address elements had newline characters. We humans do n't care abo ut newline o r tab characters. We are able to read o ver the address and interpret everything co rrectly. Tho se newlines and tabs do matter to pro grams, tho ugh. If a pro gram read tho se new elements in o rder to print an envelo pe, the results might lo o k so mething like this:

The name is indented due to the tab, and the city is cut o ff due to the extra newlines. XML schemas let yo u co mbat this pro blem by using data types. These data types do n't restrict the types o f characters used in yo ur elements and attributes, instead they tell yo ur XML parser ho w to deal with the whitespace that may o ccur in yo ur do cument. There are three string data types allo wed in schemas: xs:st ring—any characters, including any type o f white space xs:no rm alize dSt ring—characters, with tabs and newlines replaced by spaces xs:t o ke n—characters, with tabs, newlines, and multiple spaces remo ved The best data type fo r the First Nam e , Middle Nam e , and Last Nam e elements is xs:t o ke n, because we do n't want o r need any tabs o r newlines in o ur co ntacts' names. Let's update o ur schema to include this data type. Change Addre ssBo o k.xsd as sho wn:

CODE TO TYPE:

Dates No w the Birt hday element needs a data type. Mo st peo ple do n't care abo ut the time so meo ne was bo rn, just the date, so we'll use the xs:dat e data type. Update yo ur schema so it lo o ks like this: CODE TO TYPE: Dates must be in a specific fo rmat: YYYY-MM-DD where YYYY is the fo ur-digit year (like 20 12), MM is the two -digit mo nth (like 10 ), and DD is the two -digit day (like 16 ). A valid date fo r February 5, 20 13 wo uld be 20 13-0 2-0 5 . Save yo ur schema, then switch back to Addre ssBo o k.xm l and add a Birt hday element:

CODE TO TYPE: Alex Chilton 1950-12-28

and

—yo u sho uld see No e rro rs f o und.

What happens if yo u enter an invalid date? Try it. Change Addre ssBo o k.xm l as sho wn: CODE TO TYPE: Alex Chilton 01-Apr-2010

and

. That date fo rmat isn't valid, so yo u'll see an erro r like this:

Our schema pro tects us fro m entering data in inco nsistent fo rm. Switch the birthday back befo re yo u co ntinue.

Attributes No w that we've seen ho w basic data types wo rk, and we have basic co ntact info rmatio n started in o ur Address Bo o k, let's implement the next element: Gro up. This element will let us gro up o ur ho me and wo rk co ntact info rmatio n to gether.

In Addre ssBo o k.xm l, add the Gro up element as sho wn belo w: CODE TO TYPE: Alex Chilton 1950-12-28 Switch back to Addre ssBo o k.xsd and update the Co nt act element: CODE TO TYPE: Next we need to define o ur Gro upT ype co mplex type. The syntax fo r adding an attribute is nearly the same as the syntax fo r adding an element. In yo ur schema, add a new co mplex type named Gro upT ype with a single Nam e attribute, as sho wn:

CODE TO TYPE:

Save yo ur schema, then switch back to yo ur XML do cument and

. Yo u'll see an erro r:

The attribute co ntent is defined (xs:to ken) but we also need to define the co ntent fo r the element itself.

Extensions To define the co ntent allo wed in the Gro up element, we need to add an extensio n to o ur definitio n o f Gro upType. Mo dify Addre ssBo o k.xsd as sho wn

CODE TO TYPE: We are declaring that Gro upType has simpleCo ntent, which means o nly character data, and we've set the type to xs:string, which will allo w characters, line feeds, carriage returns and tabs. Save yo ur schema, then switch back to yo ur XML do cument and co rrectly, yo u will see No e rro rs f o und.

. If yo u typed everything

So , what happens if yo u remo ve the Nam e attribute fro m yo ur Gro up element? Try it. Yo u sho uld still see No e rro rs f o und. By default, attributes are o ptio nal. We can change this in o ur schema, tho ugh. Switch back to Addre ssBo o k.xsd and mo dify it as sho wn (we'll o mit so me lines here fo r brevity's sake): CODE TO TYPE: ... ...

Save yo ur schema, switch back to yo ur XML do cument and remo ve the attribute, and then, yo u'll see an erro r:

. This time

use can have o ne o f three values: o ptio nal (the default), required, o r pro hibited. Specifying use =" pro hibit e d" will disallo w the use o f the attribute o n the element. Phew! We've co vered a lo t o f material in this lesso n! In the next lesso n we'll expand o ur schema by adding mo re checks and restrictio ns. See yo u so o n! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Restrictions Welco me back! In the last lesso n we learned abo ut data types. Data types let us apply rules to o ur XML do cuments, like requiring the Birt hday element in o ur address bo o k to be a valid date, in a specific fo rmat. Data types are the initial way we specify these kinds o f rules. There are additio nal to o ls we can use to co ntro l o ur XML do cuments.

Restrictions Befo re we add restrictio ns to o ur schema, let's make things mo re interesting and add a few mo re elements and attributes. Open yo ur address bo o k XML file and yo ur schema fro m the last lesso n and let's add to o ur address bo o k. Start a new file, o r change yo ur address bo o k so it lo o ks like this: CODE TO TYPE: Alex M. Chilton 1950-12-28 123 4th Street Suite 505 San Francisco CA 94101 US 555-1212 achil [email protected] http://bigcorp.us/

Save it, and o pen Addre ssBo o k.xsd and mo dify it as sho wn:

CODE TO TYPE:



No tice that we remo ved the xs:sim ple Co nt e nt and xs:e xt e nsio n statements fro m the Gro upT ype definitio n. That's because we've added child elements to Gro up. We'll create rules fo r tho se elements next. Where did we have to add xs:sim ple Co nt e nt and xs:e xt e nsio n statements? Why?

Set of Values The address bo o k XML file we've created has a new element, the IM:

OBSERVE: achil The IM element in o ur address bo o k has o nly o ne attribute: Se rvice . Currently, any text is allo wed fo r Se rvice —it co uld be: AOL aim AIM Go o gle Jabber My Own IM Service Do n't fo rget tho ugh, aim and AIM have different meanings fo r a co mputer, because case is usually impo rtant. Suppo se we decide to restrict allo wed values fo r Se rvice to a few o f the mo st po pular IM Services: AIM Facebo o k Go o gle ICQ MSN Skype Yaho o This restrictio n wo uld make it easier fo r us to handle text, because then we wo uldn't have to wo rry abo ut which reco rds co ntain aim , AIM, aIM, o r any o ther case variatio ns. We'll specify these values in o ur schema by creating o ur o wn data type that has a restriction. We'll call this data type IMSe rvice T ype . There are a co uple o f terms that the XML Schema specificatio n uses in co njunctio n with data types and restrictio ns. Yo u're likely to enco unter these terms as yo u wo rk with XML schemas and pro grams, so yo u'll want to be familiar with them: A value space is the set o f values fo r a given data type. Fo r o ur IMSe rvice T ype , the set o f values wo uld include AIM, Facebo o k, Go o gle, ICQ, MSN, Skype, and Yaho o . A f ace t is a single defining aspect o f a value space. Facets are like the building blo cks o f a data type—they describe the data type and its allo wed values. Facets co me in two types: f undam e nt al facets let yo u specify details such as whether the data type is numeric, o r whether it co ntains a certain number o f elements. co nst raining (o r no n-f undam e nt al) facets let yo u specify the size values are allo wed to be, whether they can include white space, o r whether values are members in a set. In o rder to restrict o ur IMServiceType data type to a set o f values, we'll use an e num e rat io n co nstraining facet. Let's start by defining o ur IMServiceType data type. Type the co de belo w in yo ur Addre ssBo o k.xsd as sho wn:

CODE TO TYPE: ...

Let's take a lo o k at the new co de: OBSERVE: With this additio n to o ur schema, we've defined a new sim ple t ype named IMSe rvice T ype . It has a re st rict io n that is based o n xs:t o ke n, and its allo wed values are e num e rat e d. Take a lo o k back at the type hierarchy:

Our new IMSe rvice T ype is an extensio n o f the t o ke n simple type, which is itself an extensio n o f the st ring simple type, which is an extensio n o f the anySim ple T ype simple type, which is an extensio n o f anyT ype . Graphically, this extensio n might lo o k like:

Since IMSe rvice T ype extends (o r inherits fro m) t o ke n, its values must also fo llo w t o ke n's rules. This inheritance is po werful! Instead o f specifying every rule fo r yo ur data types, yo u o nly need to add rules to an existing set o f rules.

Switch back to Addre ssBo o k.xm l and

. Yo u'll see an erro r like this:

This erro r tells us that the current value o f the Se rvice attribute—AOL—is no t allo wed. Change it to AIM and validate again. This time yo u'll see No e rro rs f o und.

Range of Values Suppo se we wanted to restrict the allo wed values fo r Birt hday. After all, we do n't have any friends who were bo rn in the 18 0 0 s, o r after the current year. Enumerating the allo wed values wo rks well fo r the Se rvice attribute o n the IM element, but it wo uldn't wo rk as well fo r the Birt hday element. Instead, we can specify a limit o n a range o f values. To add this restrictio n, we need to add a new simple data type. Switch back to yo ur schema and scro ll to the bo tto m. Add the co de belo w as sho wn: CODE TO TYPE: ...

Take a lo o k at the co de we added: OBSERVE: ... Once again, we defined o ur simple data type, named Birt hdayT ype . It e xt e nds t he dat e data type, and has o ne rule: the minimum date is J anuary 1st , 19 0 0 . No w, to take this change "live," we need to update the data type specified in o ur Birt hday element. Mo dify the co de as sho wn:

CODE TO TYPE: ... ...lines omitted...

and switch back to Addre ssBo o k.xm l and

.

Yo u will see No e rro rs f o und—because Alex Chilto n's birthday is December 28 th, 19 50 , abo ve the minimum. Try setting it to December 28 th, 1750 , then

again. Yo u'll see an erro r like this:

This erro r tells yo u that the value yo u pro vided fo r Birt hday is no t co rrect because it vio lates the minValue facet (restriction) as specified in the schema. We also want to limit the maximum value fo r birthdays. We can do this by adding a m axInclusive element to o ur restrictio n as sho wn: CODE TO TYPE: ...

Switch back to yo ur XML do cument, and change the Birt hday to a large value, like 215 0 -12-28. Yo u'll see this erro r message:

Switch Alex Chilto n's birthday back to a no rmal value.

Length Eventually we might want to impo rt o ur address bo o k into a database. Mo st databases require yo u to specify a maximum size fo r fields like First Nam e . Since mo st o f o ur friends have sho rt names, we'll make the First Nam e field in o ur database o nly 50 characters lo ng. This restrictio n can live o utside o f the database as well when we place a length restrictio n o n o ur XML schema. Add a new simple type to Addre ssBo o k.xsd, named Nam e T ype , as sho wn: CODE TO TYPE: ... Also , update the data type o n the FirstName element as sho wn: CODE TO TYPE: ... ...

and switch back to Addre ssBo o k.xm l and standard No e rro rs f o und message.

. Since the first name is sho rt, yo u'll see the

No w change the First Nam e to so mething mo re than 50 characters lo ng—like Ale x Has a Ve ry Lo ng First Nam e T hat is Lo nge r T han 5 0 Charact e rs. Validate yo ur do cument—no w yo u'll see a new erro r:

If yo ur applicatio n requires so mething different, yo u can also use m inLe ngt h to fo rce yo ur value to have a minimum length, o r just le ngt h if yo ur value must be an exact number o f characters. Change Alex's name back to "Alex" befo re yo u co ntinue!

Pattern Values Ano ther way we can restrict the co ntent o f an element is to set a pattern value. Yo u co uld use this to ensure that the values fo r the St at e element are two uppercase letters. We'll need to add a new simple type and then define a pattern value fo r that type. Edit Addre ssBo o k.xsd as sho wn: CODE TO TYPE: ... Next, update the State element as sho wn: CODE TO TYPE: ...lines omitted... ...

and switch back to Addre ssBo o k.xm l and

name="Street" type="xs:toke name="City" type="xs:token" name="State" type="StateAbb name="PostalCode" type="xs: name="Country" type="xs:tok

.

No w change St at e fro m CA to Ca, o r so mething o ther than two capital letters. Yo u sho uld get a new erro r message. Try a variety o f different patterns. Be sure to set the state back to CA befo re yo u pro ceed.

message. Try a variety o f different patterns. Be sure to set the state back to CA befo re yo u pro ceed. What if we wanted to restrict pattern values fo r a co mplex element, like IM? Change Addre ssBo o k.xsd as sho wn: CODE TO TYPE: ... No w we just need to add a definitio n o f IMValues. Let's set it so that o nly lo wercase letters are allo wed. CODE TO TYPE: ...lines omitted...

, switch back to Addre ssBo o k.xm l, and try vario us values fo r the IM Service name, like jChilt o n, and , so yo u are familiar with the kinds o f erro rs this will pro duce. We co vered a lo t in this lesso n! In the next lesso n we'll learn abo ut different ways to structure o ur schemas to make the pro cess o f writing and using them mo re efficient. See yo u there! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Schema Structure Welco me back! We've already learned a lo t abo ut schemas. In this lesso n we'll review what we've learned, and then discuss different ways to structure XML schemas.

Names and Refs In earlier lesso ns, we added vario us data types fo r o ur elements and attributes: AddressBo o kType Co ntactType Gro upType AddressType IMType IMServiceType Defining new data types isn't the o nly way to specify rules in schemas. In fact, yo u do n't have to define any data types in yo ur schema. Instead we co uld define all o f o ur simple attributes, then refer to them later. Let's see ho w that wo uld wo rk in this little example. In XML, create a new file as sho wn: CODE TO TYPE: Alex Chilton 555-1212 This small XML file sto res a list o f friends and pho ne numbers. Save it in yo ur /xm l1 fo lder as Frie nds.xm l. No w let's set up o ur schema. Switch to a new file, and add the skeleto n schema co de as sho wn: CODE TO TYPE: Save this file in yo ur /xm l1 fo lder as Frie nds.xsd. Next, we'll define every simple element and attribute used in o ur XML do cument. Our ro o t element—Frie nds—is no t a simple element because it co ntains child elements. Pe rso n and Pho ne aren't simple elements either. That leaves First Nam e , Last Nam e , and T ype . Add definitio ns fo r tho se items to the schema as sho wn:

CODE TO TYPE:

Save it, switch back to yo ur XML file, and . At this po int, the schema is inco mplete, so yo u'll get an erro r message like Erro r: Ele m e nt 'Frie nds': No m at ching glo bal de clarat io n available . Line : 4 . Switch back to yo ur schema. Next, we need to define o ur Frie nds element, a co mplex type. Add the co de belo w to yo ur schema as sho wn: CODE TO TYPE: Instead o f defining a separate data type fo r the Frie nds element, we'll define it "inline." This co de is similar to co de we presented in the Intro ductio n to Schemas. Pe rso n is the o nly child element o f Frie nds, so we'll add it to o ur schema as sho wn:

CODE TO TYPE: No w we can define the two child elements o f Pe rso n. Type the co de belo w as sho wn: CODE TO TYPE: Here we specify that First Nam e and Last Nam e are allo wable child elements o f Pe rso n. The re f (sho rt fo r reference) specifies that the full definitio ns o f First Nam e and Last Nam e o ccur elsewhere in the schema—in this case, they are at the beginning o f the file. No w we can add the related co de fo r the Pho ne element and attribute. This time we'll add a new element definitio n at the beginning o f o ur file and use a re f :

CODE TO TYPE: Finally, we're able to add the Pho ne element to the rules fo r Pe rso n. Type the co de belo w as sho wn:

CODE TO TYPE:

Save it, switch back to yo ur XML file, and

. Yo u sho uld see No e rro rs f o und.

Building a Schema At this po int, yo u may wo nder when to use yo ur o wn data types and when to use ref. Ho w sho uld yo u structure yo ur schema? Well, that depends. Go o d schemas (alo ng with go o d co mputer pro grams, web pages, and even bo o ks) are defined by the ease with which yo u can understand and maintain them. Ultimately, it is up to yo u as the schema autho r to decide ho w to co de yo ur schema. Take a lo o k back at the definitio n fo r Frie nds:

OBSERVE: This element definitio n is getting pretty co mplex, even tho ugh its related XML is relatively sho rt. Here's ho w it wo uld lo o k if everything was defined inline: OBSERVE: This element definitio n wo rks exactly the same way as the prio r definitio n, but it's much lo nger. If yo u find yo ur schema is getting unwieldy, yo u can always refactor yo ur co de—rewrite it to make it mo re efficient and easier to understand.

Substitutions Let's go back to o ur little Frie nds.xm l file:

OBSERVE: Alex Chilton 555-1212 Suppo se we have many Spanish-speaking friends who also want to use this XML structure, but might prefer to use Am igo s instead o f Frie nds. We can enable them to do that. Change Frie nds.xm l as sho wn: CODE TO TYPE: Alex Chilton 555-1212

and . Yo u'll see a message like Erro r: Ele m e nt 'Am igo s': No m at ching glo bal de clarat io n available f o r t he validat io n ro o t . o n line 4 . This so rt o f difference can be acco unted fo r in a schema by using substitutions. Switch to yo ur schema and add this line:

CODE TO TYPE: We defined a new element named Am igo s that is a substitute fo r Frie nds. To test it, save yo ur schema, switch back to yo ur XML file, and validate. Yo u'll see "No erro rs fo und." Substitutio n can be pretty handy, but it do es have limitatio ns. Substitutio n do esn't wo rk fo r attributes, and it o nly wo rks fo r global elements. In o ur schema, First Nam e , Last Nam e , Pho ne , and Frie nds are all glo bal elements, because each related xs:e le m e nt is a direct child o f xs:sche m a.

Any Our friend list is pretty small; we can o nly specify each friend's first and last name and pho ne number. Suppo se we want to allo w any additio nal elements under Pe rso n. Ho w might we do this? Switch to yo ur XML do cument and add a new element as sho wn:

CODE TO TYPE: Alex Chilton 555-1212 Alex1092 Validate yo ur do cument; yo u see a message like Erro r: Ele m e nt 'IM': T his e le m e nt is no t e xpe ct e d. o n line 9 . To get aro und this pro blem witho ut actually listing all o f the allo wed child elements o f Perso n, we can use the any element. Switch to yo ur schema and mo dify it as sho wn: CODE TO TYPE:

Save it, switch back to yo ur XML file, and

. Yo u sho uld see No e rro rs f o und.

The pro ce ssCo nt e nt s=" skip" attribute o n any directs the XML parser to skip validatio n o f elements, essentially allo wing any elements o r attributes to be placed in this part o f the XML do cument. Other values fo r pro ce ssCo nt e nt s are:

st rict : any element can be used, but the element must be specified in the schema fo r the required namespace. lax: like strict, but no erro r will o ccur if the schema canno t be fo und. By default, st rict is used. We've co vered a lo t in this lesso n! In the next lesso n, we'll shift gears and discuss transfo rmatio ns using XSL. See yo u there! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Basic XSL What is XSL? So far we've o nly used XSL behind the scenes to sho w ho w XML can be translated into HTML to view in yo ur web bro wser. Yo u kno w eno ugh abo ut XML no w to begin experimenting in XSL. To reiterate, XSL stands fo r eXtensible Stylesheet Language. In English, that means XSL is an XML do cument that co ntains instructio ns fo r taking a so urce XML file and transfo rming it into so mething else. This "so mething else" co uld be HTML, XML, text, o r all o f tho se. XSL is o ne o f the mo st impo rtant uses o f XML—it allo ws a single so urce XML do cument to be transfo rmed in many different ways. In still o ther wo rds, XSL is an XML do cument, so we must fo llo w all o f XML's rules when creating XSL files.

Getting the Most Out of XSL XSL can be used to transfo rm XML, but what else can it do ? We can use XSL to add so me so rting capability so o ur XML pho ne bo o k info rmatio n will be returned in alphabetical o rder. This is especially useful when we have several peo ple accessing o ur Pho neBo o k.xml do cument. Peo ple who use o ur pho ne bo o k file may add pho ne bo o k listings at the to p o f the file, the bo tto m o f the file, o r they may even try to alphabetize the list themselves. Instead o f depending o n users to so rt the info rmatio n themselves, we can use an XSL do cument to translate the so urce XML pho nebo o k into an alphabetically o rdered pho nebo o k (in HTML o r XML). This is o nly o ne o f many po ssible uses fo r XML. Try a Go o gle search fo r Uses Fo r XSL to find mo re.

Using the T ranslate Function in CodeRunner Co deRunner has a po werful Translate functio n. Let's take a minute and go o ver ho w it wo rks. Open yo ur Frie nds.xm l file and mo dify the co de as sho wn: CODE TO TYPE: Alex Chilton 555-1212 Alex1092

Linking XSL to your XML Source Yo u used xml-stylesheet statements in yo ur xml files back in yo ur first XML do cument, so the new co de pro bably lo o ks familiar. When wo rking with XML and XSL do cuments, yo u need to specify which XSL do cument will be used to translate yo ur XML do cument by default. The xml-stylesheet statement serves that purpo se. Here's the co de we might use to link XSL inside yo ur XML do cument: OBSERVE: . . . In this example, we added a reference to the XSL file de f ault .xsl, which makes that o ur default XSL file.

If yo u were to o pen yo ur XML file in a web bro wser, it wo uld auto matically perfo rm the translatio n using the default.xsl style sheet and the results wo uld be displayed o n yo ur screen. No w, click to translate yo ur XML file. Yo ur XML do cument is translated and the results displayed in a new windo w. Yo u'll see an example o f ho w the page will display in the Rendered tab and the raw co de the bro wser is wo rking with in the So urce tab:

Layout of an XSL Document and T emplates Creating XSL files is similar to creating XML files. Let's get started by taking a lo o k at the text that is required fo r yo ur XSL do cument: OBSERVE:

What do these lines mean? Well, refers to the versio n o f xml being used, and the standards that are being applied to the do cument. xsl:st yle she e t ve rsio n=" 1.0 " indicates the versio n o f the style sheet definitio n yo u're using (similar to the versio n o f XML yo u are claiming to fo llo w). xm lns:xsl=" ht t p://www.w3.o rg/19 9 9 /XSL/T ransf o rm " declares the name space yo ur XSL do cument is using. Finally, xsl:o ut put m e t ho d=" ht m l" / tells o ur translato r that we are o utputting HTML text as o ppo sed to plain text. /xsl:st yle she e t is the style sheet's clo sing tag. Make sure yo u include this clo sing tag when yo u editing yo ur files in Co deRunner! It's easy to fo rget.

More About xsl:output The xsl:o ut put tag isn't actually required by all pro cesso rs, but it's a go o d idea to include it anyway. Even if yo ur pro cesso r igno res the tag, a human who tries to read yo ur XSL do cument will no t. So what do es this tag do ? It tells the pro cesso r (and human readers) the "target" o f yo ur translatio n. There are three majo r types o f xsl:o ut put targets: Cho o sing between the XML and HTML tags may seem excessive at times, since bo th XML and HTML are actually text files, but so me extra magic may o ccur, depending o n which o f these o utput types yo u cho o se. Fo r instance, if yo ur XSL do cument is go ing to co nvert yo ur XML pho ne bo o k to an HTML file and yo u set yo ur o utput type to "html,", yo ur pro cesso r may add so me extra info rmatio n to the HTML do cument to make it easier fo r web bro wsers to pro cess. Yo ur pro cesso r may also try to indent the resulting do cument if the o utput type is HTML. If yo u are translating yo ur do cument to XML, yo u'll want to indicate yo ur DOCTYPE o r schema as well. If yo ur DTD is external, yo u can add a do ct ype -syst e m attribute to yo ur xsl:o utput element. To specify yo ur external DTD fo r yo ur XML o utput metho d, yo u'd do this: OBSERVE: To specify yo ur schema fo r yo ur XML o utput metho d, yo u'd do this: OBSERVE:

xsl:template No w we can get to wo rk o n o ur basic XSL do cument. To practice. we'll apply XSL to o ur existing XML data. Let's wo rk with o ur address bo o k and get it to print o ut HELLO every time it hits an entry. Create an XSL do cument by typing the co de belo w in Co deRunner as sho wn:

CODE TO TYPE: HELLO Save this file in yo ur /xm l1 fo lder as Addre ssBo o k0 1.xsl; then, change Frie nds.xm l as sho wn: CODE TO TYPE: Alex Chilton 555-1212 Alex1092 Translate the do cument. Yo u sho uld see a "HELLO" o n the screen. Once yo u get that example wo rking, try adding ano ther Pe rso n to yo ur XML do cument. Translate the do cument again, and see what happens. Surprised? The tag defines a set o f instructio ns fo r the root element o f yo ur XML so urce file. (Remember, the root element is the first element that o ccurs in an XML do cument. In this case, the Frie nds element is the ro o t element.) When the ro o t element is matched against this template, HELLO is o utput. Only o ne HELLO is ever o utput, because there is o nly o ne ro o t element.

xsl:apply-templates is o ne o f the mo st frequently used tags in XSL. When translatio n so ftware enco unters a tag in yo ur XML file that matches the tag defined by yo ur template, it will "execute" the XSL instructio ns lo cated within the xsl:template tags o f yo ur XSL do cument. If yo u place yo ur o wn XML tags o r text inside the template, they will be co pied to the resulting do cument. Yo u define a set o f instructio ns within xsl:template tags, and they are executed fo r the tags that yo u have designated in yo ur so urce XML do cument. This is kno wn in the XML wo rld as push-processing, because yo ur XML do cument is "pushed" thro ugh the instructio ns. The XSLT pro cesso r "pushes" the so urce tree no des thro ugh the stylesheet, which has template rules to handle vario us kinds o f no des as they co me thro ugh. In the next lesso n, yo u will learn abo ut pull-processing with . Here is an example o f usage: Exam ple

What it do e s



Applies templates to all elements in the do cument.



Applies templates to all children o f the current element.



Applies templates to Pho ne elements that are children o f the current element.

Note

Yo u can use inside o f a template. When the template is applied to an element, all text inside the element is co pied to the resulting do cument.

Let's try printing o ut XML IS FUN when yo u enco unter a tag. Mo dify Addre ssBo o k0 1.xsl as sho wn:

CODE TO TYPE: HELLO XML IS FUN

Save this in yo ur /xm l1 fo lder as Addre ssBo o k0 2.xsl, make the appro priate change to yo ur xml file, and then . Yo u sho uld see o ne instance o f "XML IS FUN" created fo r each Perso n tag in yo ur XML do cument. Try adding mo re elements to the XML do cument and translate again. Let's go o ver this co de in detail. OBSERVE: HELLO XML IS FUN Yo u may remember what xsl:t e m plat e m at ch=" /" do es, but what do es mean? When the XSLT pro cesso r is chugging thro ugh yo ur style sheet, it will pro cess the ro o t element first. Fro m there, it enco unters the instructio n . This pro mpts the pro cesso r to lo o k fo r the t e m plat e to match "Perso n". It finds all Pe rso n elements that are children o f Frie nds (because o f the //) and applies the template to it. Try replacing with . Instead o f matching Perso n elements belo w the ro o t element, this instructio n will match Pe rso n elements at the ro o t level. In a "File System" view, the difference might lo o k like this: Wit ho ut //:

Note

Wit h //:

When wo rking with large o r co mplex XML do cuments, it's helpful to draw a quick sketch o f yo ur XML do cument and its layo ut, so yo u can keep track o f the lo catio n o f the elements in yo ur do cument.

I think that's eno ugh fo r no w! Spend so me time experimenting with yo ur xml and xsl files, and I'll see yo u in the next lesso n. Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Advanced XSL T ranslating the Address Book to XHT ML Yo u have a basic understanding o f ho w XSLT wo rks. No w we're go ing to build an XSL file that will translate yo ur address bo o k into an XHTML representatio n. Let's start with a basic XSL do cument that will print o ut o nly the basic XHTML stuff: the , , and elements. Our go al is to create this co mplete XHTML do cument. Let's get started with yo ur address bo o k XML file. Change Amigo s back to Friends and add ano ther Perso n to the file, as sho wn: CODE TO TYPE: Alex Chilton 555-1212 Alex1092 Laura Chilton 555-5678 LaurethSulfate Yo u'll reco gnize o ur standard address bo o k. No w let's match the ro o t element o f o ur XML do cument and start creating the basic XHTML. Start a new file and type the co de belo w as sho wn: CODE TO TYPE: My Address Book

Save it in yo ur /xm l1 fo lder as Addre ssBo o k0 3.xsl. Switch to Frie nds.xm l and it. No thing is rendered, but if yo u lo o k at the So urce o f the translatio n, yo u'll see the beginnings o f o ur XHTML do cument. In the previo us lesso n, we matched the ro o t element and the o utput was "HELLO." This XSL is similar, o nly the o utput co nsists o f so me XHTML. This is a great beginning. No w let's co nsider o ur address bo o k's child element, Pe rso n. We need to add a template to match o ur Pe rso n element and then add the appro priate xsl:apply-t e m plat e s. The template can go anywhere in o ur style sheet; we'll put it at the bo tto m this time. We want o ur data to be displayed within the bo dy tags in o ur resulting XHTML do cument, so we'll place the xsl:apply-t e m plat e s within them. Mo dify the XSL file as sho wn:

CODE TO TYPE: My Address Book person

Save the XSL file and translate the XML do cument again. This time yo u sho uld see "perso n perso n"—o ne instance o f the wo rd "perso n" is created fo r each Pe rso n element in o ur XML do cument. No w let's talk abo ut Perso n's children: First Nam e , Last Nam e , Pho ne , and IM. We need to add new templates to co rrespo nd to these elements. We can add them anywhere in o ur do cument. Let's just add them to the bo tto m. We want to apply the templates fo r these elements inside o ur Perso n element, because they are children o f Perso n. Let's just wo rk with First Nam e , Last Nam e , and IM fo r no w. We'll add Pho ne in a minute. Mo dify Addre ssBo o k0 3.xsl as sho wn: CODE TO TYPE: My Address Book first last im

No thing new here. and No w o ur fo undatio n is in place.

again. Instead o f seeing "perso n perso n," yo u see "first last im first last im."

In o ur previo us lab we learned that we can print o ut the co ntents o f an element by using the xsl:apply-t e m plat e s instructio n. Let's do that with o ur do cument. We'll replace the "first," "last," and "im" text in o ur XSL with the new

xsl:apply-t e m plat e s instructio n. Mo dify Addre ssBo o k0 3.xsl as sho wn: CODE TO TYPE: My Address Book

and again. Yo u see "AlexChilto nAlex10 9 2LauraChilto nLaurethSulfate." This do cument is ugly! Fo rtunately, we can fix that. We'll encapsulate the inside so me XHTML by putting the o utput in tags and adding so me css styles to make o ur text lo o k like the example here. While we're at it, we'll add line breaks after Last Nam e and IM to pro vide so me space. All o f o ur changes need to be put inside the template fo r Pe rso n. Make sure to clo se yo ur br tags co rrectly. Mo dify Addre ssBo o k0 3.xsl as sho wn:

CODE TO TYPE: My Address Book

()

and again. This is the best-lo o king address bo o k we've created so far. Experiment! Mo ve the spans and brs fro m the Pe rso n template to the IM template. Yo u'll see similar results. Try adding so me o ther css—perhaps fo nt-weight o r backgro und-co lo r, o r so mething else to jazz up this do cument a bit. Ho w wo uld yo u add spaces between the names? This metho d o f translating to XHTML is time-co nsuming, but no t particularly difficult. Still, there must be an easier way! What abo ut attributes? Our Pho ne element has a T ype attribute. If we wanted that in the o utput, ho w wo uld we write the XSL?

xsl:value-of Fo rtunately, in XSL, there's an instructio n that o utputs the value o f an element OR an attribute: xsl:value -o f . We can use it to replace the xsl:apply-t e m plat e s fo r FirstName, LastName, and IM, as well as their co rrespo nding templates. If we replace xsl:apply-t e m plat e s with xsl:value -o f , the o utput will be the value o f an element o r attribute. Mo dify yo ur XSL do cument as sho wn:

CODE TO TYPE: My Address Book
()


This text is much sho rter and mo re clear!

and

again. The result is exactly the same as befo re.

Finally, let's add suppo rt to print o ut the T ype attribute o f the Pho ne element. Mo dify yo ur XSL do cument as sho wn: CODE TO TYPE: My Address Book
()
:
Mo st o f the changes will be familiar to yo u no w, except maybe the Pho ne/@Type. We are interested in Pho ne 's T ype attribute, but no t Pe rso n's. The @ symbo l indicates that we are interested in the attribute T ype , no t the element. Had we entered Pho ne /T ype abo ve instead, we wo uld have go tten the value o f Pho ne 's T ype element (which do esn't exist). Had we included o nly @ T ype , we wo uld have go tten the value o f Pe rso n's T ype attribute (which do esn't exist). and again. Did yo u no tice what happens when there's no T ype attribute? Suppo se we decided that if there is no T ype attribute, the Pho ne wo uld auto matically be Ho m e ? Ho w can we acco mplish that?

xsl:if We'll start with an if st at e m e nt . If yo u kno w almo st any pro gramming language, yo u're familiar with the if st at e m e nt . If no t, fear no t! An if st at e m e nt tests to find o ut whether a co nditio n is true o r false. The result o f that test determines whether a subsequent actio n will take place in yo ur pro gram. We can use the if st at e m e nt to determine whether the Type attribute exists, and if so , whether it has a value. If the Type attribute exists and is no t empty, we will o utput it.

Basic Syntax for xsl:if So ho w do yo u use xsl:if ? Let's take a lo o k: OBSERVE: TRUE A bo o lean (true o r false) test is perfo rmed o n the text within the t e st at t ribut e . If the result o f that test is true, everything in blue is evaluated. Unlike in o ther languages, this co nstruct do es no t allo w fo r an ELSE st at e m e nt . Instead, yo u must use xsl:cho o se (yo u'll see this in actio n sho rtly).

Syntax for T est xsl:if requires its T EST attribute to be bo o lean. Many things can go into the T EST attribute—co mpariso ns between numbers, strings and values o f elements, o r attributes themselves. Here are so me examples o f T EST elements and what they do : T e st Co ndit io n

De script io n

@Type='Ho me'

If the Type attribute is equal to Ho me, this expressio n is TRUE.

Type='Cell'

If the Type element is equal to Cell, this expressio n is TRUE.

xsl:if with our Address Book Remember what happened when we translated o ur address bo o k with the last XSL file when there was no T ype attribute specified? We can fix this by using . Mo dify Addre ssBo o k0 3.xsl as sho wn:

CODE TO TYPE: My Address Book
()
:


and

.

Using wo rks to a po int, but it do esn't print o ut the default value o f Ho m e when no T ype attribute is present. In that case, it do esn't print anything. Since do esn't o ffer an "else" clause, we canno t use it to print Ho m e when no T ype attribute is present. We'll have to use instead.

xsl:choose The xsl:cho o se co nstruct wo rks like this: OBSERVE: statement a statement b statement c In this example, the first test is perfo rmed. If it is TRUE, then st at e m e nt a is executed. Otherwise, the next test is perfo rmed (if it exists). If that test is TRUE, then st at e m e nt b is executed. Otherwise (as yo u may have guessed), st at e m e nt c is executed.

Note

When using xsl:cho o se , yo u can have as many xsl:whe n sectio ns as yo u need.

Edit yo ur Addre ssBo o k0 3.xsl as sho wn:

CODE TO TYPE: My Address Book
()
: Home:
We added the extra xsl:whe n clause required if the T ype attribute is no t set. No w add ano ther xsl:whe n sectio n to o utput (Ho me) when the T ype attribute exists, but is empty.

xsl:for-each In XSL, there are two different ways to co mplete the same pro cessing task. Instead o f push-pro cessing, so me peo ple prefer pull-processing. Pull-pro cessing can be acco mplished via the xsl:f o r-e ach tag. This is similar to the "fo r lo o p" co nstructs in many o ther languages. In push-processing, yo ur XML elements are essentially pushed thro ugh the templates as they are matched. In pullprocessing, yo ur XML elements are matched first and then the templates are applied to them, o ne at a time. Let me give yo u a visual representatio n o f these pro cesses. Say yo u're do ing laundry and yo u want to fo ld yo ur SOCKS and iro n yo ur SHIRTS. If yo ur laundry style is push pro cessing, yo u'll grab clo thes at rando m. If yo u enco unter a SOCK, yo u fo ld it immediately, and if yo u enco unter a SHIRT yo u iro n it immediately. If yo ur laundry style is pull pro cessing, yo u grab all o f yo ur SOCKs first and fo ld them, then grab all o f yo ur SHIRTs and iro n them. Let's try using xsl:f o r-e ach. Begin by co nverting Addre ssBo o k0 3.xsl fro m push-pro cessing to pull-pro cessing, as sho wn:

CODE TO TYPE: My Address Book
() : Home:

()
: Home:


No w,

and

. This translatio n perfo rms identically to the previo us translatio ns.

So what happened with o ur XSL exactly? Let's break do wn the new co de. Yo u may remember the template to match the ro o t element. We o utput so me co mmo n XHTML, then we enco unter . This line selects every Perso n element in o ur XML do cument. One by o ne, tho se elements are pro cessed using the instructio ns within that indented blo ck. The clo ses the xsl:fo r-each tag pro perly. Next there are so me XHTML clo sing tags, template clo sing tags, and finally the style sheet clo sing tag. Experiment with xsl:f o r-e ach. When yo u feel like yo u've go t a handle o n this co ncept, save this XSL file.

template-match or for-each? So , no w that yo u've learned abo ut each o f these tags, which o ne is better to use in which situatio n? There is no co rrect answer. Yo u're the bo ss—use the metho d yo u like better, the o ne yo u find clear and easier and to read. Always strive fo r simplicity and readability. Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

XPath In this lesso n, we'll examine XPath, the XML Pat h Language .

What is XPath? XPath is the language that lets yo u navigate an XML do cument and extract elements and attributes fro m that do cument. We actually used XPath in the last lesso ns when we discussed XSLT—the XPath co nsisted o f the bits o f co de we used to select elements and attributes. Let's take a lo o k at the XML file we created back in the Schema Structure lesso n fo r Frie nds.xm l: OBSERVE: Alex Chilton 555-1212 In XPath termino lo gy, an XML do cument fo rms a tree o f nodes. (Remember back in Yo ur First XML Do cument, we discussed the tree structure o f XML.) A node is any element, attribute, o r bit o f text in a do cument. In tree fo rm, the do cument abo ve lo o ks like this:

The / indicates the ro o t o f the XML do cument. At the ro o t is the Frie nds no de, which has o ne child no de o f Pe rso n, which has child no des o f First Nam e , Last Nam e , and Pho ne . The First Nam e no de has a child text no de with a value o f Ale x. The First Nam e , Last Nam e and Pho ne no des are all siblings. The Pho ne no de has a child attribute no de named T ype .

First Nam e has a parent no de o f Pe rso n, and a grandparent no de o f Frie nds. Co mpare this to a directo ry o n yo ur co mputer. Yo u might have a fo lder fo r Frie nds, which co ntains a fo lder fo r Pe rso n, which co ntains fo lders fo r First Nam e , Last Nam e , and Pho ne :

If yo u are familiar with Unix, Linux, o r Macinto sh co mputers, yo u co uld write a path to the first name like this: OBSERVE: //Friends/Person/FirstName/Alex In the Windo ws wo rld, that same path wo uld lo o k like this: OBSERVE: C:\Friends\Person\FirstName\Alex Just like we use paths o n o ur co mputers to specify lo catio ns o f files, we use XPath to specify lo catio ns o f XML elements and attributes that interest us. XPath isn't just fo r XSL—it's also useful in any applicatio n that reads o r writes XML. Remember the sho rt XSL file we created in Basic XSL? Here it is: OBSERVE: HELLO Yo u may no t have realized it, but we used XPath in o ur element. The fo rward slash / is the XPath expressio n fo r the ro o t no de o f an XML file.

Using XPath to Navigate an XML Document Create a new XML file and co py the co de belo w (just this o nce, yo u can co py and paste it):

CODE TO TYPE: Alex Chilton 555-1212

it in yo ur /xm l1 fo lder as Frie ndsX.xm l. Suppo se we want to retrieve the first name o f a perso n in o ur friends list. Ho w can we make that happen? In a new XSL file, type the co de belo w as sho wn: CODE TO TYPE:

it in yo ur /xm l1 fo lder as Frie ndsXPat h.xsl, switch back to Frie ndsX.xm l, and yo u'll see the first name:

. Sure eno ugh,

OBSERVE: Alex Our XSL file displayed the resulting no de o f the XPath expressio n o f /Frie nds/Pe rso n/First Nam e . So , what if o ur XML do cument has multiple peo ple in it? Mo dify Frie ndsX.xm l as sho wn: CODE TO TYPE: Alex Chilton 555-1212 Jim Dandy 555-1313

and

again. Yo u'll see so mething familiar:

OBSERVE: Alex What happened to Jim? Our XSLT engine o nly displays the first result, even tho ugh there are multiple no des in o ur do cument. In o ther wo rds, o ur XPath expressio n resulted in a node set instead o f a single no de. A node set is a list o f no des. To get aro und this, we can use to iterate o ver each no de in the no de set. Mo dify Frie ndsXPat h.xsl as sho wn: CODE TO TYPE: First Name:


and

again. This time, yo u see bo th names:

OBSERVE: First Name: Alex First Name: Jim In this XSL file, we lo o ped o ver the results o f o ur XPath expressio n—/Frie nds/Pe rso n/First Nam e . The XPath expressio n in the value -o f element is a single perio d (.) and is used to indicate "current no de." In this co ntext, the current no de is each as pro cessed by . If yo u have used the ls co mmand o n a Unix o r Mac co mputer, o r the dir co mmand o n a Windo ws machine, yo u've pro bably seen o utput that lo o ks like this: OBSERVE: cold:~$ ls -al /Friends/Person/FirstName/ total 0 drwxr-xr-x 3 certjosh certjosh 102 Jun 22 14:22 . drwxr-xr-x 6 certjosh certjosh 204 Jun 22 14:23 .. -rw-r--r-- 1 certjosh certjosh 0 Jun 22 14:22 Alex cold:~$ OBSERVE: C:\dir Friends\Person\FirstName Volume in drive C has no label. Volume Serial Number is EC14-6B6E Directory of C:\Friends\Person\FirstName 06/21/2010 06/21/2010 09/01/2009

C:\>

01:22 PM . 01:22 PM .. 06:14 AM Alex 1 File(s) 0 bytes 7 Dir(s) 2,632,855,552 bytes free

Bo th o f these listings have special entries which are nearly identical in the XPath wo rld: Ent ry

Dire ct o ry Me aning

XPat h Me aning

Single perio d .

Current directo ry

Two perio ds

Parent o f current directo ry Parent o f current no de

..

Current no de

Armed with this new info rmatio n, let's experiment and change Frie ndsXPat h.xsl slightly as sho wn: CODE TO TYPE: First Name:
Phone:


and

. No w yo u see the pho ne number asso ciated with each perso n:

OBSERVE: First Name: Alex Phone: 555-1212 First Name: Jim Phone: 555-1313 There is o ne mo re distinctio n we sho uld make with XPath expressio ns. An XPath expressio n can be absolute o r relative. When the expressio n is abso lute, the expressio n is a full path that po ints to a specific place in the XML tree, independent o f the current lo catio n. When the expressio n is relative, it is dependent o n the current lo catio n. Absolute expressio ns always begin with the ro o t no de (in o ur case, Frie nds) o r a fo rward slash (/). Relative expressio ns start with the descendant o f the ro o t no de. Our XSL do cument has three XPath expressio ns: 1. / - the ro o t no de 2. /Frie nds/Pe rso n/First Nam e - an absolute path 3. ../Pho ne - a relative path The seco nd XPath expressio n is abso lute, because it begins with a fo rward slash. It always results in a list o f First Nam e no des. The third XPath expressio n is relative, and is dependent o n po sitio n. In o ur example, we use it in co njunctio n with the seco nd XPath expressio n. Tho se two expressio ns to gether can be expressed this way: /Frie nds/Pe rso n/First Nam e /../Pho ne ...which sho rtens to : /Frie nds/Pe rso n/Pho ne In English we'd say, "given a FirstName no de, find its parent, than find that parent's child Pho ne element."

Predicates As we said earlier, a no de set is a list o f no des that is the result o f an XPath expressio n. Because it is a list, we can use a special no tatio n to pick o ut items fro m the list. Let's try it. Mo dify Frie ndsXPat h.xsl as sho wn:

CODE TO TYPE: First Name:
Phone:

and

. This time yo u will see o nly Jim:

OBSERVE: First Name: Jim Phone: 555-1313 We used a Predicate (the square brackets and number [2]) to select the seco nd perso n fro m o ur XML do cument. XPath uses o ne-based indexing, meaning that lo catio ns elements begin at po sitio n o ne. If yo u are familiar with languages such as C o r Perl, yo u may no tice the structural difference—tho se languages typically use zero -based indexing instead o f o ne-based indexing. XPath also has a special functio n that lets us pick o ut the last no de instead o f hard co ding a number. Try it: CODE TO TYPE: First Name:
Phone: Add ano ther perso n to Frie ndsX.xm l as sho wn:

CODE TO TYPE: Alex Chilton 555-1212 Jim Dandy 555-1313 Neko Case 555-2323

and

again. Yo u see the last perso n in the list:

OBSERVE: First Name: Neko Phone: 555-2323 We co vered a lo t in this lesso n! In the next lesso n we'll discuss additio nal ways to use XPath to navigate thro ugh an XML do cument. See yo u there! Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

Advanced XPath Welco me back! In the last lesso n we learned ho w to use XPath to navigate an XML do cument. In this lesso n, we'll explo re several o f the functio ns available in XPath, and learn additio nal ways to traverse do cuments.

Functions Let's get right to wo rk. Open Frie ndsX.xm l fro m the last lesso n and remo ve the last perso n we added: CODE TO TYPE: Alex Chilton 555-1212 Jim Dandy 555-1313 Neko Case 555-2323 No w, suppo se yo u want to kno w ho w many peo ple (Pe rso n no des) exist in o ur do cument. Yo u can find o ut with XPath using the co unt functio n. Let's give that a try. Open yo ur Frie ndsXPat h.xsl fro m the last lesso n and mo dify it as sho wn: CODE TO TYPE:

and yo ur XML do cument. Yo u sho uld get a result o f 2. The //Pe rso n XPath translates in English to "any o ccurrence o f the Pe rso n element in the do cument." So , what else can we do with co unt ? Let's say we want to see the names o f the elements that have two children. Mo dify Frie ndsXPat h.xsl as sho wn:

CODE TO TYPE:

and

. Yo u'll see this:

OBSERVE: Friends This is the o nly element in o ur do cument that has exactly two children. Great, but ho w did o ur XPath expressio n wo rk? Let's break it do wn: OBSERVE: //*[count(*)=2] The first characters—//*—literally mean "any element in the do cument." When we discussed predicates earlier, we learned that the square brackets [ ] indicate that we are using a predicate to pick no des fro m a no de set. In o ur current example, instead o f picking a specific index like 1 o r 2, o ur predicate selects no de s wit h t wo childre n with the expressio n co unt (*)=2. Putting this to gether in English, the expressio n //*[co unt (*)=2] wo uld read, "select all elements fro m the do cument that have exactly two child elements." We used o ne additio nal functio n here—nam e (). This functio n returns the name o f the element. We're just getting started. We can have a lo t mo re fun with XPath! Ho w abo ut selecting elements who se names co ntain "Name?" Sure, why no t? Update Frie ndsXPat h.xsl as sho wn: CODE TO TYPE: Element name:


and

:

OBSERVE: Element Element Element Element

name: name: name: name:

FirstName LastName FirstName LastName

Once again we used a predicate to pick specific no des fro m o ur no de set o f "all elements." This time we used the co nt ains functio n to make sure that the result o f nam e () co ntained the wo rd Nam e . No w suppo se we want to select all elements with names that do not co ntain the wo rd Nam e . This is po ssible to o . Mo dify Frie ndsXPat h.xsl as sho wn: CODE TO TYPE: Element name:


and

yo ur do cument. Yo u'll see all o f the elements yo u didn't see befo re:

OBSERVE: Element Element Element Element Element

name: name: name: name: name:

Friends Person Phone Person Phone

Here we used the no t () functio n to negate the meaning o f the enclo sed predicate expressio n. So metimes the questio ns we want answered aren't so straightfo rward, tho ugh. What if we want to co mbine o ur prio r questio ns so we can see elements with two children, as well as elements with names that co ntain the wo rd "Name"? XPath has yo u co vered! We can use the pipe (|) to co mbine two XPath expressio ns. Mo dify Frie ndsXPat h.xsl as sho wn: CODE TO TYPE: Element name:


and

:

OBSERVE: Element Element Element Element Element

Axes

name: name: name: name: name:

Friends FirstName LastName FirstName LastName

The functio ns we've seen so far allo w us to write so me pretty co mplex expressio ns, but they wo n't answer every questio n. Take a lo o k at o ur XML do cument: OBSERVE: Alex Chilton 555-1212 Jim Dandy 555-1313 XML do cuments fo rm "family trees," co mplete with parents, children, and siblings. That means that the two Pe rso n elements in o ur do cument are siblings. If we wanted to access these two elements as we might o ther siblings, ho w co uld we navigate to a sibling using an XPath expressio n? We'd do it using an axis. In XPath, an axis defines a no de set, relative to the current no de. Let's get started. Write an XPath expressio n using a predicate to pick the very first Pe rso n no de. Edit Frie ndsXPat h.xsl as sho wn: CODE TO TYPE: The first person's first name:

and . Here we used an XPath expressio n with predicate /Frie nds/Pe rso n[1] to select the first Pe rso n element, and then displayed that Perso n's First Nam e , which is Alex. No w that we have selected the first element, ho w can we select the sibling? We'll use the f o llo wing-sibling axis. Mo dify Frie ndsXPat h.xsl as sho wn: CODE TO TYPE: The first person's first name:
The second person's first name:

and

:

OBSERVE: The first person's first name: Alex The second person's first name: Jim The expressio n wo rks, using this syntax: OBSERVE: axis::nodetest[optional predicate] Our specific expressio n is: OBSERVE: following-sibling::*/FirstName Our axis is f o llo wing-sibling—which refers to all sibling elements that o ccur after the current element (a better name might have been following-siblings). The no de t e st is */First Nam e —which means "all elements with a FirstName child element." We are no t using a predicate, so that sectio n is o mitted fro m o ur expressio n. What do yo u think wo uld happen if we had mo re than o ne sibling fo llo wing Alex in o ur xml do cument? Let's give it a try. Mo dify Frie ndsX.xm l as sho wn: CODE TO TYPE: Alex Chilton 555-1212 Jim Dandy 555-1313 Janie Jones 555-1414

and

:

OBSERVE: The first person's first name: Alex The second person's first name: Jim What's go ing o n? Altho ugh f o llo wing-sibling refers to all sibling elements that o ccur after the current element, xsl:value -o f o nly returns the value o f the first fo llo wing sibling. To get all the names o f the fo llo wing siblings, layer ano ther fo r-each statement in yo ur xsl file:

CODE TO TYPE: The first person's first name:
The following sibling:


and

:

OBSERVE: The first person's first name: Alex The following sibling: Jim The following sibling: Janie Yo u might wo nder why we used this f o llo wing-sibling axis instead o f using a do uble perio d (..) like we did in the last lesso n. I'm glad yo u're thinking abo ut stuff like that! Let's try it—mo dify Frie ndsXPat h.xsl as sho wn: CODE TO TYPE: The first person's first name:
The following sibling:

and

. The results aren't quite what we're lo o king fo r:

OBSERVE: The first person's first name: Alex The following sibling: Alex Alright, then, let's use a predicate! Mo dify Frie ndsXPat h.xsl as sho wn:

CODE TO TYPE: The first person's first name:
The following sibling:

and

. Yo u'll see better results:

OBSERVE: The first person's first name: Alex The following sibling: Jim If yo u are feeling uneasy abo ut this last expressio n—go o d! Hard-co ding that 2 in the predicate may seem like a go o d idea no w, but with mo re than two Pe o ple elements in yo ur do cument, yo u'd have to add ano ther line with the predicate [3] to see Janie! In pro gramming, there are o ften many different ways to acco mplish the same go al. Clear, easy-to -understand co de is always the best o ptio n. While many aspects o f axes can be duplicated using "no rmal" XPath Expressio ns, axes are usually mo re easily understo o d. f o llo wing-sibling is much clearer than ../Pe rso n[n]!

Other Axes f o llo wing-sibling is no t the o nly axis in XPath—in fact there are several: Axis

No de s se le ct e d f ro m current node.

ancesto r

All ancesto rs, including parent and grandparent.

ancesto r-o r-self

All ancesto rs, including parent and grandparent and t he curre nt no de .

descendant

All children, grandchildren, great grandchildren, etc.

descendant-o r-self All children, grandchildren, great grandchildren, etc and t he curre nt no de .

fo llo wing

Everything af t e r the clo sing tag o f current no de.

preceding

Everything be f o re the o pening tag o f current no de.

fo llo wing-sibling

All siblings after current no de.

preceding-sibling

All siblings befo re the current no de.

child

All children.

parent

Parent o f current no de.

self

Current no de.

attribute

All attributes.

namespace

All namespace no des o f current no de.

There's a lo t to digest in this lesso n. In the next lesso n we'll switch gears back to XSLT and discuss so me recent updates in the newer XSLT 2.0 standard. See yo u there!

Copyright © 1998-2014 O'Reilly Media, Inc.

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. See http://creativecommons.org/licenses/by-sa/3.0/legalcode for more information.

The future of XML XSLT 2.0 / XPath 2.0 In previo us lesso ns we learned to use XSLT to transfo rm o ur XML do cuments into o ther representatio ns. We also co vered XSLT Versio n 1.0 —the first release o f the XSLT specificatio n. XSLT 1.0 (and the related XPath 1.0 ) wo rk really well, but they have their sho rtco mings. W3C, the gro up that designs XSLT, addressed so me o f these issues in the versio n 2.0 o f XSLT and XPath. Versio n 2.0 has been released fo r a while no w, but as o f 20 10 it has no t been widely ado pted. Majo r languages such as PHP and C#/VB.Net do no t o ffer native XSLT 2.0 pro cesso rs. (There is a 2.0 pro cesso r available fo r .Net and Java, tho ugh: Saxo n) Similarly, majo r web bro wsers such as Firefo x and Internet Explo rer o ffer XSLT 1.0 functio nality, but do no t o ffer XSLT 2.0 yet. So , ho w can yo u tell if an XSL file uses versio n 2.0 ? Take a lo o k at these few lines o f co de: OBSERVE: ...rest of style sheet... Fo r versio n 2.0 we use two namespaces: o ne fo r XSL, and o ne fo r sche m as. Finally, we set the ve rsio n to 2.0 .

Changes Found in Version 2.0 Data T ypes One majo r change in versio n 2.0 is the inclusio n o f XML schema's data types. In versio n 1.0 , there were fo ur data types: Strings Bo o leans No de sets Numbers In versio n 2.0 , there are many additio nal data types, such as: xs:integer xs:decimal xs:string xs:date Data types are serio usly useful. One o f the best examples o f this can be fo und when handling date and time data. Suppo se yo u have two dates with times that refer to a library bo o k: OBSERVE: No w let's say that yo u want to display the number o f days that the bo o k was checked o ut. If yo u're using XSLT 1.0 , yo u'd need to parse the dates and times manually and then perfo rm date arithmetic to get the info rmatio n yo u need. But if yo u're fo rtunate eno ugh to be using XSLT 2.0 , yo u can use date functio ns to retrieve yo ur info rmatio n fast! Let's give this a try. Create a new XML file as sho wn:

CODE TO TYPE:

Save it in yo ur /xm l1 fo lder as bo o k.xm l. Next, create a new XSL file as sho wn: CODE TO TYPE: The book was checked out for: days. Save it in yo ur /xm l1 fo lder as bo o k.xsl. Right no w, Co deRunner suppo rts o nly XSLT 1.0 . In o rder to take advantage o f XSLT 2.0 features, we can use Saxo n fro m the Unix co mmand line. Switch to a terminal by clicking the T e rm inal ico n ( ). Yo u may be asked to accept the security certificate fo r MindTerm, the ssh client we'll be using in this co urse. Go ahead and accept it. Then yo u'll be lo gged in to the OST server. The server is named co ld. All OST students have shells o n this server. The shell is where Unix co mmands are executed o n the server. The co mmands yo u execute o n yo ur shell will no t affect any o ther shell. co ld:~/xm l1$ is what's kno wn as a "co mmand pro mpt." If yo u see co ld:~/xm l1$ , yo u're ready to execute Unix co mmands. (To lo g o ut o f the terminal when yo u finished, type e xit at the co mmand pro mpt.) Type this co de at the Unix pro mpt: INTERACTIVE SESSION: cold:~/xml1$ cd xml1 cold1:~/xml1$ saxon9he.sh -xsl:book.xsl book.xml

If yo u typed everything co rrectly, yo u'll see this: OBSERVE: The book was checked out for: P17DT18H33M17S days This text represents the duration o f time between the check o ut and return dates—in this case, 17 days, 18 ho urs, 33 minutes, and 17 seco nds. If we want to see o nly the number o f days, we can use the days-f ro m durat io n functio n. Change bo o k.xsl as sho wn:

CODE TO TYPE: The book was checked out for: days. Run saxo n again in the Terminal windo w (yo u can repeat the co mmand by pressing the up arro w). Yo u'll see this: OBSERVE: The book was checked out for: 17 days

Output In Basic XSL, we discussed the