---
title: WWDC2004 Session 417
framework: wwdc
role: article
path: wwdc/wwdc2004-417
---

# WWDC2004 Session 417

## Transcript

Kind: captions Language: en ladies and gentlemen please welcome software engineer Sarah Wilkin good morning everyone so I can probably guess why you're all here which is I got this quote from this you know the cocoa mailing list and someone asks well that's me am I missing something or if they're actually no xml object for cocoa well I mean there isn't until today so what you've had so far is there's the sax parser and with that you basically get a bunch of call back as you go through your XML and that's useful to an extent where you want to build your own custom objects but often you just want that final thing at the end and then there's CF XML tree which has a lot of limitations in particular it doesn't have any sort of namespace handling so again these are some more quotes from the developer list people are asking not just for an XML objects but they're asking oh well you know I want xpath and I'm want xquery and xslt and I wanted to fix my malformed XML and also I you know really love if it could do transformations and all sorts of magical things so that's what I'm going to give you today we're going to introduce and s xml so what is an f xml well it's a set of five classes in foundation you can produce xml from scratch you can read in an xml document or you can start from HTML convert it into xml find all your leaf nodes converted you know pull things out put other things back in and then just in one line of code you can apply xquery which is a new w three standard or XSL see this is all you need to know if you know a little bit of cocoa because i'm going to be showing you quite a bit of code later on and this in Safari you can go view source and say hey you know this is the text element I'm looking for this is the attribute I'm looking for then you should be good to go if you're starting from scratch also you just should know something about the DTD or the schema so you can build valid XML so XML it's everywhere right everywhere you look so you have XML to straight XML if you go to the w3 org website you'll see there's all these different standards and you can go all over the web and find this information everywhere and we want you to use it right then on top of that you have that file formats so you know kino actually stores its data as XML it's not even export it's actually already xml and illustrator you can escort as SVG and final cut pro which we're going to be showing later on then you have t lists and also services so scraping I know you all do it and I'm telling you try to put a stop to that so you know as I mentioned you can you can do this custom parsing which is not that great but otherwise your left scraping these strings out and you're trying to create this XML but you know you're going to end up forgetting an end tag or you're going to do something a little bit wrong and then somebody else wants to use your xml and it's not formed so please stop use this instead this is NS XML so the top level we have NF xmlnode which everything inherits from so what covers just your basic things attribute namespace processing a session and text and these things may or may not have a name and they'll have an object value and say where they are in the tree then you have NF XML document which holds everything and it will also have a version and a character encoding then there's NS XML element where you have your list of attributes end to your list of namespaces very important and for those of you that are into DT DS we have NSX ml DT D where you have your system and public ID and it stores a whole bunch of dte nodes so they'll tell you what can be valid or not so before I talk more about this let's show you some code so it's really easy you just have your URL and then there's three different ways you can start creating your document you can either in it with contents of URL and it was data or if you've created a route elephants from root element from scratch just use that so I'm going to show how easy it is by walking all the way over to this demo machine so here I have basically the same lines of code I was just showing and what I'm doing is I'm getting this from apple.com / RSS so as you may know RSS is really big saying we just announced Safari RSS and there's all different information out there available to you so normally now that Safari RSS is here if we click on this we get this cool view but we just want the data so i saved this URL and this is it here and then I just alloc init and I'm turning on pretty printing which is a special option to make all the indenting go in and then there's also a root element on the document so I'm going to go straight into the debugger and let's be freakin make sure you can see this all right all right so let's say I want to print out I said I has a character encoding right well utf-8 we have a version okay and then there's that route which has a name and it also has attributes and then faces a child count and of course there's all the things you'd expect like child at index and replace child index all right let's go back to slides so instead of doing this please NS XML is for cocoa lovers use this be happy instead of doing this custom parts of a socks we want you to use these risk us objects instead of doing all the scraping just use the methods that we have instead of being difficult it's easy and instead of it being error-prone and malformed hey we'll even fix it up for you so this is you know instead of apples starting from scratch obviously we're going to use these great libraries that are already out there so already live XML tool is in the system for passer well now we have lib xslt and tidy then on top of that we have this you know all of our standards so if you go to the w3 home page and look at their specifications well we've tried to adhere to that pretty well and as I mentioned we've got this whole xquery engine in there which I'm going to show you some xquery later on because I think it's a really powerful language for processing XML and previously if you've developed any sherlock channels that's what we were using it in we were using xquery and javascript for channels and last year we had a sherlock session there was like hey you know why can't I use xquery outside of Sherlock well here you go and then msxml encompasses all of them so we're going to go into a pretty long demo but before we get there this is sort of what I think is what you're regularly going to do with msxml if you have some data out there you find it you're you're pulling a little bit out of it and then you have some sort of template and you're pushing in that new data now i'd like to invite Elena do up to the stage Elena works on final cut which can now import an export XML with final cut our way to do we're not ready for the demo messina with your box blood thank you very much okay let's just say what a problem is well it's a typical problem you've got some sort of person an artist that's done a really great template and then you have this database or you have all the information somewhere and you want to put it in so you know typical sort of separation of content and display so in particular I already sold you the itunes music store RSS and we're going to use that again for this demo so we won't have some sort of template where it has the song name artist name the rank and we're going to add in some new information which is the rank change so you know so I spiders bugs me that I can check out well what are the top 10 songs but are they new today or did they change from yesterday's number one going down or is it going up so we're going to add that in to go to the RSS and then this is what we want to make is a put this information into the template so now we'll go to the demo machine and this one way we could do this is just make some simple XML and apply a style sheet to it and so far I can actually deal with that so you have this processing instruction that says this is my style sheet and you don't even need to bother writing HTML and it can look great so we've made this demo for you that will be available at approximately 6 30 this evening so you can try this out yourselves and we're just going to modify it a bit to make it better for stage so we're just going to build and run this and here we go so this is XM elves of style fee it's not HTML so let's just check out view the source right so we're going to do something really similar to this but you know the web it's good to a certain point can we go back to 5s but really we want to do something more exciting we want to make a video so we're going to do something a final cut pro and dead so I just want to give you a summary of sort of the algorithm we're going to use so you don't get lost along the way so this is that what the RSS looks like I briefly showed it or no I'm just showing it to you now for the first time so we have a lots of items and this is namespace itms so instead of just being straight regular RSS we've added in this namespace and it contains a whole bunch of information attached to it we have the artist name album the link and we're going to be using the link as our sort of unique ID to compare between the two different versions of the XML then we're going to use xquery and extract out the information we want and at the same time make the XML a lot simpler to understand so this is at the point that i left the XML for the sample code that you'll be looking at so this is sort of as far as you go and then you apply to stop soon you're done but here we're going to go a bit further where we're actually going to go into final cut pro and export some xml from template and then pull all those things together so our data source as i mentioned xml is everywhere there's lots of places to find it I really encourage you to if you find one data source to keep looking because the coolest applications of this is really when you can bring together lots of different sources so instead of stopping it showing the top 10 well maybe I should go look up information on the artist or you know find related tracks and also display that we've already found the data source so we're not going to bother showing you that on the demo machine next we are going to extract the information so here we are going to use xquery and of course use ex Corey we need to know a little bit about ex Corey so xquery at its basic is it incorporates X half a next path is quite straightforward so if you're in your shell and you can move up and down directories you can do basic dot is your context item just like it is in your directory structure if you want to go dot slash with a name well that goes one element down the tree so say I when dot slash a that would give me all my links if I went dot / /a that would give me every single link in the tree instead of just the children names that match and dot dot goes up so that's really straightforward but then we can add these predicates sentence which makes it much more powerful because inside a predicate you can have any excess or ex Corey function you want and there's really a lot of them which I'll show you in a minute or two so you can do string matching including regular expressions based on the perl-compatible regular expression library and will help match based on what kind of element it is text or attribute so we'll go back to the demo machine now and show how easy this can be so with our our itunes RSS let's here it is in objective-c we could say oh give me my first child move down give me my next child walk through the tree you try to find this try to find all the items and the items are could you just scroll down a bit yes so we have this whole series of items and if all you have is items as your children well that's fine but what if there's a comment in between well now suddenly you have to walk through all of the children just looking for the ones of their items so xquery lets you know cuts out this whole stuff so sweet go to the xquery tab we want to find all the items all we type is dot slash slash items and there they are and say we want all of these song names on those items well that's itms colon song okay there's all of our song names and say we want the song links will we decide on link so it's really it's that simple I mean that's a lot easier probably than whatever you've been doing so if we can go back to slides now now if we want to so as mentioning all the functions here some of them there's around 144 of them I really encourage you to use ex Corey when when dealing with XML is a more fully fledged language so if we want to do something to each of those items that we need some sort of iterate or right and then ex Corey that's called the flower statement so it's really similar to an SQL your you know select order by I think that's going on forever I like hex queria long so your flower statement here you have for let order by where and return and a lot of xquery you'll see we'll just have a whole bunch of foreign let statements so for is your iterator and let is your assignment statement then you've ordered by and you always need to have a return which is sometimes confusing for new people to the language you can't go let let and return what you have you have to have the return you have a really big ex Corey file we also encourage you to put it in a separate file which will be doing whereas if you just have a one-line xpath you should just write it straight within the code go back to our demo so I'm back in NSX in my browser we just want to show a simple flower statement before we get into the code here so if we want to loop over all of the items you don't say for dollar items dollars the way of denoting variables next query and we're going to grab each of the song name and also get the album title and then concatenate them together and just return that and then join the whole sequence together with returns so if we run that there we go so in an ex Corey everything is a sequence even if it's mine item it's still sequence of one and you can't nefse quences so in our return statement if we'd written a return of a sequence there it would just end up appending them all together just a little dot shot that you want to be watch out for so now we're going to go straight into the code and go back to I talked about Steph one but we didn't show the code because the code is so similar to what i did in the first demo so here we are getting our old RSS and our new RSS and these are sort of pre-canned versions I saved I think one of them is from last Friday and one is on Monday so that will be the two lists that we're comparing and we go to step two step two is where we're at actually applying the xquery so here we're getting as I mentioned we want to store it in a separate file because her ex Corey is quite large which we're going to go through another minute the NS bundle we're describing it and then the next thing you'll notice is this Constance dictionary so ex Corey as I mentioned since it's just a one line function call it can only be really apply to your current you know that one object that you're calling it on so we can say new RSS object for xquery but we need some way of getting the old document in there so to do that we have this constant dictionary and this key is going to match something that we declare in the xquery so it's a way of just passing information in between four from objective-c and text query and then our the last line is our one line that we do to actually apply the xquery and we're going to get back this big sequence of items so now I'm instead of looking at the xquery in Xcode which has no X query syntax coloring we're just going to look at a canned RTF file there it is so X great comments are these really cute smiley faces so you're only happy when you're working on xquery so this is what I saw him about the first line here this is where we're bringing in that object that we declared in our objective c so now we've got these two documents one is the context item which is doc and then the other one is dollar old Doc's then we're going to loop through so this is like just a really big version of the small snippet of X quarter showed you just a couple of minutes ago so we're getting a few more attributes than we did before in addition to the song and song link we have the artist album and you'll see here where it goes / text parenthesis that's matching a text node so you can also have / attribute / element so it's really easy to pick out will say I want all the comments I go fast comment then we want to find how the old list comparison in your list so there's that song link remember which we can use is the unique ID so we're using this predicate statement but based on the song link in that entire array of old items it's going to find just the one that matches and of course there's the chance that doesn't match longest brand new and we'll deal with that so then we have this little if statement you know standard stuff and we're just building up this string that says well did the song go up a notch or down to is it brand new or is unchanged then the last thing we do if you can see that far helaina join us move the whole thing up a bit whole window great up ok there we go so another great thing about xquery is instead of having to write in line you know left blah blah blah right and make sure you match your start and end names is they have these constructors so I did this is an element constructor it's going to build an element with some attributes and our attributes for giving special names here because we're going to use these to match the names that are in Final Cut Pro and here also anything above it can be destructive is you can put any variable you want in there it's I mean it's not like hard-coded XML alright so the very end of this hour loop is done we've created this list it's a list of items which have these four attributes which contain information from the original RSS plus the rank change and now if we can go back to floods so now Final Cut Pro we want to create our template so again there's lots of different ways of creating this template if you look at applications now they can do so much XML that it's crazy and when you go and illustrator and you've created this beautiful graphic and then you can export it as SVG and programmatically you know turn it into whatever you want and like I'm really excited about that because that's one of the annoying things for me if you get into this program and instantly you want to do something really repetitive especially in Final Cut Pro and now you can just export the whole thing use a little bit of NS xml and xquery and import it back in and it's all done for you so this is a couple things we're going to be looking for in our exported Final Cut Pro XML a generator item is going to be our text elements and then the item will be vegetable text go back to the demo machine here we are in final cut pro I know for most of you like me this was my first time using final cut pro for this presentation and it was really fun and it wasn't that difficult to get something going and then once I had this little snippet there well then I could manipulate it with my mad objective c programming skills and you don't build this whole really cool presentation so here we have our template item and we've got these for text elements each on their own track so this is important to keep in mind because what we're going to do is duplicate each of these along the track so we're moving forward in time and we're going to change this to actually be a real countdown and then there needs to be up a little bit of audio and a little bit of video to spice it up so we can just export this and final cut pro it actually exports everything that you do in it you think well maybe you know I'm doing this really cool transition and it's not going to be able to describe in XML but you know helaina and others have done such an excellent job it's all there so we'll just take a look at what the exported file looks like so we're giving it this name that we want to remember when we're back in our code and we're just going to search for one of those strings there it is so if we scroll up a bit until we hit a generator item so there's that generator item this is our generated piece of text and above that is track so that's the track that the generated item is on so what we want to do is take out this template item because we really don't want something that says song name and change something inside it then push it back in multiple times for each of the four things on there for different tracks now if we can go back to slides okay this is our last step I'm realizing now you'll be out quite early so we're going to insert our change coffees so we had our xml that we created from xquery and we have this xml that we've exported from Final Cut Pro then we're going to put it all together we're going to use some of these n FX and malfunctions so on the left-hand side you have all the sort of attributes of your things and then on the right-hand side you have everything dealing with children so I mean as you can probably guess these things end up being kvo compliant so we'll go back to the demo now all right so oh I forgot of stuff step 3 okay after we exported the Final Cut Pro XML and I showed you where that generator item and the track are now we want to find those so we're importing the template then we have our array with the four keys and remember we named in Final Cut Pro we named our text with the same names with the attribute names that we export from xquery so we're just going to loop through each of those four things and we want to build one array it's the generator items and another array of the tracks because ultimately the generator items are going to disappear from the original and we want to assert things into the tracks there's that little bit of xpath there what tries to explain so we want to find within that big exported file just that one particular generator item right and to do that we go you look for all the generator items then we have this predicate we're inside there we look for a text element that matches based on the name of wherever we are inside our array then we add the generator object and also its parent to text notes and track notes the last thing we do in the loop is detached it which is you know obviously another msxml question which just takes something right out of the tree basically setting it's apparent to nail and deleting it from the from the parent okay step 4 slow last up so you see lengths there this is so we can move ahead of time we're going around 30 frames with back down we want each thing to play for two seconds then we get that first item so we've got these ten items from ex Corey remember so that's current then we're looping through our for special keys and the first thing we do flick it is the actual generator item because that's the lonely one with serve our new text in then we come up with our replacement string which is the attribute with the same name matching one of those keys and now we find just the text node so we do a little bit more xpath just to find the text no because remember we don't want to replace the whole text of the generator item we just want our place that the text and where it belongs so we set the string value and then that dot saw start and dot slash ends those are certain end times we want to make sure that we have to move forward in time and don't end up with our ten clips on top of each other then we add our child and release it and we're done so I mean that's that's really all there is to it so let's go back let's let's run this so it see the variance that we just write it back to a file okay and then we can go back and find without pro and import this and it should have our 10 bits from the RSS in Final Cut moving along in time there they are so we can see we've we've added that additional information of you know whether it's unchanged or moved up and down so let's I just switch to our app reek and rendered version of this and play it back if your looks like [Music] yeah made a whole movie with XML at what's the name of the band again be expendable from Santa Cruz they're really nice they lent us this footage and their music so we're not stepping on anyone's feet now let's say I go back to flood so in addition to everything i just showed you i just want to show you a few more header highlights so in addition to exporting xml we're also can export XHTML or HTML and at this point i will encourage you to read the release notes because there's some things that aren't quite finished and there's a couple knowing bugs that we don't want you are hitting upon but there's generally workarounds that are known you can also set string value resolving entities so this is something that i mean i really like because it means in your DTD you can just say hey this string should resolve to this other string and then pass it in with entity and have it resolve for you so you don't have to parse through your string and replace it later on i showed you objects for ex Corey there's also object by applying excess ltd which is you know another great language / technology it's sort of in theory similar to ex Corey that I think X squares a bit more powerful an xslt you can also do some other things like you can export RTF or you can export PDF depending on how you write your xslt so that's why I'm its object by applying xslt instead of document because you can get either enough data back or you can get an NS XML document then there's also resolved namespace for name as I mentioned we put a lot of work into having the namespaces just work so when you add something and we'll actually go and find the namespaces associated with which means you can say hey what's the name space for barca land baths and we'll say oh sh t TR com then we have fidelity which for those of you that are using straight lib XML to right now is something I know you're not getting because it was quite a bit of work to get this in there so what I mean by fidelity is just that your input you know you probably want it to look like your output so if you've got white space in between your elements you might want to keep that if you use single quotes or double quotes on your attributes you know you might have some reason that you want to keep those around and prefixes entities so all of this stuff also keep in mind though that this will be a performance hit I've tested this if you turn on all of these options you'll get a boat of fifty percent performance it on per se so just use the ones that you really need and bindings so you briefly saw the little NS XML application and since we have a minute i'm just going to show you a little bit more of it if we go to the demo machine so there's a whole tab here which i didn't show you which is where the whole TV oq vc comes in so we have our source here and there's this editor tab and you can just move down the tree those up the tree and edit things here you can see there's some name station attributes and find the information about the document and it really didn't take me long to put this together it was an exercise and seeing if her bindings works and they do I mean it's great it's basically you know you can build a whole XML editor really quickly and it also wouldn't take much effort if you guys went to the what's new and cocoa talk they said that there's new bindings for trees you should be able to just put an extra class in there that steps to this and be able to do an outline view editor you know in a day and let's go back to slides so that's it I want to see what you can do we're giving you all of the stuff it you know there's that option NS XML document tidy HTML which takes your HTML and converts it into validate XML you can also tidy up XML and that you may have guessed we're using natural HTML tidy library if you've heard of it salsa vailable deviated there's are five classes and ass XML document node element DTD and dte node and one of the first ex Corey engines as i mentioned xquery is now its final call happened in February so it's really getting to be a stabilized language we don't expect major things happening like they have been the past couple years and please file bugs I know of a bunch but you're probably going to find a bunch more so I can't find out about them unless you file them so get on your bug reporter follow them in also enhancements we know that we haven't covered everything they are interested in I'm especially interested in hearing what people want more from the DTD stuff and validation a Matthew formica is our cocoa evangelist he should be your first sort of email contact and then there's jungle NZ who you probably met at the H hi talk we have a bunch of great documentation that Terry put together who you'll see in QA in addition to the regular foundation web page in a way you'll see the new classes there's also this whole you know prose based hey what's msxml and it goes into how xml browser was put together how the bindings go together little code snippets and there's the two things I showed you the top 40 that first version with CSS and XML browser which I just showed you a little bit more of the full code is available for that it will be up on the server around six-thirty today and w3 is really the definitive resource for all of this because xquery is a pretty new line good you should really go to them for but more tutorials or coming up I'd like to thank you very much
