02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

More Then Just Leftovers

In this session, we discuss some miscellaneous topics mainly concerned with what is and isn't legal content in XML documents, and also ways to circumvent these rules using special element constructs. The topics we cover are:

  1. Comments and how they behave
  2. White-space handing
  3. Special characters in content
  4. Creating parser-ignorable content
  5. Embedding version and character-set information

We will illustrate these concepts with an example, "The King's Speech", based on the multi-Oscar winning 2011 British Film of the year. This movie addressed King George VI's speech problem, and its correction by a speech therapist. Let us now see the King in modern times, with the BBC making a recording of his World War III speech to motivate the British Army troops stationed across the world. Here is how the BBC's state-of-the-art speech recording and filtering software would go about its task, while choosing XML to be the output format:

Stammering speech is stored as comments in the XML document. (This content is useful for creating statistics and studying them.) Long periods of silence (no noise) are treated as 1 white-space per second of silence. Special characters display mathematical statistics. Recording noise is also identified and isolated, but maintained in the document as special non-processed data, for the tool's own logging and improvement. Here is the document example:

<?xml version="1.0" encoding="UTF-8" ?>

<king-speech>

<!%u2014line> duh-duh-duh (This is Stammering) duh-duh </line -- >

<line>The time has come for the British to stand up for the rest of Humanity</line>

<line> </line>

<![CDATA[ALL RECORDING NOISE*****]]>

<!%u2014line>The rest of the speech</line -- >

<statistics>Stammer Percentage &lt; 30 per cent</statistics>

<statistics>Noise Correction &lt; 45 per cent</statistics>

</king-speech>

Here is how we map the syntax used in this document to the actual requirements, stated just preceding it:

  1. Comment Syntax : <!%u2014 is the start tag and -- > is the end-tag. Anything in between is ignored by the application, so this is ideal for storing the king's stammering to be analyzed later on.
  2. White-space: considered as valid content%u2026 here used to model silence in the speech.
  3. The recording noise is a binary bit-pattern which cannot be parsed by the XML parser as it can only understand character data. We could have ignored it, but decided to keep it for statistical purposes. The start tag is : <![CDATA[ and end-tag is ]]>
  4. Special mathematical symbols such as '<' can be confused for beginning of a tag, so special character sequences such as '&lt;' (familiar from HTML) are used to preserve them.
  5. The first line of the XML document describes the type of information (XML,) the version of the XML standard (1.0) and the encoding information (UTF-8).

The main point is that XML understands and stores only character sequence data, and has a few restricted keywords. But it also provides constructs to work around these limitations and syntactic rules, as we saw in the document example above.

Video Transcript

00:00All right, so let's talk a little bit about some of our loose ends things that are important to XML
00:06But it's hard for us to see it at this stage of our training
00:09All right
00:10So the first thing is there's another option with XML which is basically the exact same thing as we just learned
00:16We just talked about attributes and one of the possible attributes is the ID attribute
00:20Which is a little bit special than other attributes and the ID attribute doesn't have any specific
00:27Functionality inside of XML, but then a lot of programming languages lend on IDs
00:32just as for example with HTML you could have a div and that div could have an ID of a certain name now that name that
00:44Name is then referenced in the CSS or in JavaScript and that way you could easily access that specific div
00:51Same is true with XML
00:52So you can use the ID tag and that ID tag will then make it easier for you to fetch that information right now
00:59It's a little bit theoretical. So that's why we're running through it really quickly instead of digging deeper into that concept
01:06Now the next thing I wanted to talk about is talking about comments and XML
01:10For example, if we just go back to one of our previous jobs in a second
01:15Let's say we have this this node, but we don't really want to completely delete it
01:19We just want to comment it out for now and have our application ignore it and for the meantime
01:24To do that all we have to do. It's very simple
01:26We start if we just look at the structure itself of it is it's smaller than
01:33Then we put here a command. I don't even know how to call it minus minus. So that's basically our sign out anything
01:45anything inside here is
01:48commented now whenever you're done with your comment the way you end your comment is again with minus minus and then greater than
01:56All right, so that's all you have to do to create a comment
01:59So for example in our scenario with our div we could just go into the div itself and just add those characters
02:04so this is our opener and
02:06This is our ender and
02:09That way we made this whole div light div commented out now in the same rule. We could have just set
02:17The beginning here and just leave it all the way to flow so everything will be commented out
02:21So all of this will be commented out because it encounters our closing right here now
02:27You can't have any spacings. You can't put a space here or space there
02:30It has to be all connected together
02:32And you can't put a comment inside of a comment because as soon as it's commented
02:36That's it. You're already in a comment mode
02:38An XML contrary to some other programming languages
02:41There's no such thing as a comment of one line or a comment of multiple lines
02:46You always set the beginning of the comment and the end of the comment, so it would actually look this way
02:53So anything in between here would be the comment area
02:57All right, so that's about comments now
03:03it's obviously important because you might for example get into your code and
03:06You are now working on your code and suddenly you decide that you want to comment out this specific student
03:12so all you have to do is just as we said earlier just comment that and
03:17Then comment that and that made it. This is because this is the opening
03:22This is the closing so anything inside of here will be commented out or if I undo this
03:27Let's say we want to keep the Spanish, but for now we want to comment Spanish out, and it's that easy
03:32We just find the beginning of it and the end of it, and we have commented out
03:37Alright now I want to talk about one more thing and that one more thing is white spaces
03:42Contrary to HTML by the way this commenting mythology works in HTML as well
03:47So it's the same exact thing now
03:50HTML contrary to HTML does not ignore white space and HTML if you put a lot of line breaks
03:56If you just break a line a couple of times HTML will ignore it
04:00XML doesn't so by default every single break of a line means something
04:05So but this means that there's actually content inside of here besides our complex content, which are is our nodes, right?
04:11We have your our nodes now suddenly we have a fourth element and maybe a fifth six seven so suddenly we have your data
04:17That's not only our nodes, so it's very important not to put extra space you could break a line obviously breaking a line is
04:25Not going to affect anything if it's between
04:28Nodes, but on the other hand if we put here a few break lines
04:32We might create some issues now one more thing to note is we didn't have to break a line this visual beautiful
04:38Structure could have not been there if we wouldn't have put that break line, and we would just set it up this way
04:43It would be the exact same thing only visually would probably be harder for us to see now a lot of programming languages actually come with
04:50A built-in white space condenser, so it really depends on that specific language that you're working with it within
04:56But for example if you break a line here that just means that all of this is now part of your node
05:02It does not mean that you just have 40
05:04Okay, so there's there's definitely an importance to noticing these stuff
05:09now our next topic that I want to talk about really quickly and I'm really talking about this quickly because you're going to encounter these issues as
05:16You start working with characters that are not in English or you start working with characters that are just technically
05:23Not the English language such as what if you need to use a greater sign inside of your text
05:29What if you have to write here 80 greater than 20?
05:33You're not allowed to nor are you allowed to do smaller than because what might happen?
05:37Our XML parser would think that this is the beginning of the name of the of another node
05:42And then you'll see that our XML is broken
05:46So instead of that they're saying that's called
05:48Entities now our entities we basically instead of putting greater than and we're gonna dig a little bit more deep into it as we start
05:55Seeing our XML in reality
05:58We would we would type end and
06:02Then whatever we want so if it's like greater than so it'll be greater than and then we're gonna close it with the semicolon
06:09So all of this actually comes to replace just a greater than now
06:14You don't have to memorize these stuff. There's actually five built-in ones which are the greater than
06:22less than
06:25There's the
06:28Ampersand there's the apostrophe. There's the quotation mark so for example
06:33What if he had an attribute here such as name and you wrote my name, but my name is Ben's house
06:41Now notice that this is fine because I'm using a single quote inside and a double quote out
06:46So the parser is smart enough to know that because I started with a double quotation
06:51I need to end it with a double quotation now if I use a single quotation
06:56It doesn't really care now on the other hand
06:58What if I had to use a double quotation inside if I needed to use a double quotation inside if I needed to use a quotation
07:04mark inside
07:06The way I would do it
07:07I would have to use one of those entities that we were talking about so one of those
07:11Entities would be the quote one now the quote one works this way
07:15So again, we put end and then we would just write Q U O T and then
07:21comma
07:24Let me colon actually. All right, so and that would create our quote
07:27And you don't have to memorize that that you could always google it if you remember the word entities
07:31You could always google it or you could go always start a course page and find the actual entities in our page itself
07:38All right, and there's a lot of other entities which you could find and discover as you go on and work on things
07:44But what I wanted to do is kind of wrap these kind of more
07:48I'm sure you're gonna revisit this video later on as you start programming
07:51It's just important notes to know about so we could say that we know all the basics of XML
07:57So we talked about comments. We talked about white space. We talked about
08:02Areas where you can't use specific characters because really XML is only English if you start using characters that are not in English
08:08XML will start getting
08:10confused
08:11So one of the seven for example if I have in my math area here and I need to start writing something
08:17I might need to write something in French or I need to use a lot of different math equations
08:21Maybe I need to type all these stuff inside. How could I put anything I want inside of my math area?
08:27Well, it's fortunately for us. It's quite easy. Well as we spoke before
08:32XML is really parsed XML goes through and it builds structures. It builds a tree. We need to tell it our XML parser everything between
08:42This part to that part everything in this area. We don't want it parsed now look how we actually do it
08:48There's a few steps here
08:49So let me open up our bigger screen for a second so we could see it nice and big
08:54So our steps are this way first of all we have a greater than or smaller than whatever you call it depending on the
09:00Direction that you're looking
09:03All right, that's our next character our next character, and then we're gonna write see data and
09:09Then we're gonna open one more
09:11So this is a lot of typing and it's okay if you don't memorize it if you remember the see data
09:15You could always look it up or come on our site and look at a cheat sheet, and you'll find it
09:19These basically typing all this will actually open up our area of you could put here
09:25Whatever you want now how you close it is very similar so first of all we just
09:30Close our first bracket close our second bracket, and then close our greater than so this is our closing bracket for a see data
09:37And this is an opening for a see data
09:39All right, so if we go back into our application now. We could actually type in
09:46And I encourage you to always use it and then we could put here
09:48Whatever we want and it's not going to parse it
09:52And you're not going to hit those errors now as you start building your app like XML you are going to fall on
09:57XML errors some of them will be easy for you to check to figure out and fix immediately some of them will be a little
10:04bit harder and
10:05You're always encouraged to bring up issues and talk about them if it's on our site or any forum
10:11It's a great way to practice and learn and end of the day the smaller details really when it comes down to language and actual
10:17implementation are really where you're you're going to build that skill and obviously every time you fall and
10:23And then figure out your solution
10:25That's basically how you're going to become better and better programmers and obviously better XML editors now besides that XML is quite simple
10:33And really the places where you'll really have the challenges in building XMLs is really when you start hitting those
10:39non
10:40Expected characters that XML doesn't approve of
10:44Alright now the last topic and I already made space for it the last topic that I want to talk about in this the in
10:49This video about all the stuff. We wanted to talk about really quickly is about
10:54Now some programming languages don't really care about it, but this is just really the standard anytime you build an XML file most modern editors
11:02And we'll look at the modern editors at future videos such as Dreamweaver in the modern editors
11:07They will automatically appear so let's see how we actually type it in so first of all
11:11We're going to open up a smaller than than question mark
11:14space XML
11:17well XML version equals
11:26Now you might be thinking to yourself. Why should I do it to be honest I?
11:31Just do it. I don't never ever really change it
11:34You know you're it's one of those stuff that you just put inside there
11:37You could probably ignore it most chances, but it's just this is how you're supposed to do it
11:42You're basically declaring to whatever is going to look on this file
11:45What type of file it is and we're basically declaring and saying?
11:49This is an XML file the versioning of XML is 1.0 is really that's the version that we're in with XML
11:55you could also add encoding so if you know you're encoding and
12:00Really, I'd probably encourage you to put this as well try to aim for UTF
12:0516 that will make sure that you have all the characters you would ever need most chances and
12:10That really covers it so you why do we put this in this is kind of like a intro to the file letting the whatever is
12:17Looking at it if it's directly the browser letting it know what to expect in the file
12:21What are the character types which is basically letting our operating know what type of characters what fonts to be using well more accurately what?
12:30If it's going to be Unicode if it's going to be whatever it's going to be now
12:34I mean this is kind of like a you know like a word a bigger world of its own
12:37So I wouldn't worry about it too much until you start hitting
12:40Problems and you're going to start hitting those problems when you start working with non-English characters
12:45and the next step is just the version so really that really covers in a lot a lot of programming languages that are kind of
12:52Node-based if it's HTML if it's XSLT if it's whatever it is a lot of times
12:57You'll have in that first line a declaration of saying what it is
13:01Because after we save our file if I just you know click on to save the file as I save the file
13:07Really, I'm just going to save it right on top of my desktop. Yeah, why not?
13:12So if I save it right on top of my desktop and forgive me for all the different files that are on there
13:16And I just save the file. I'd want to save it as an XML so maybe I'll call it school
13:21That XML so that's my XML file and now I have a completed file
13:26Which is an XML file and notice how the editor now that it knows it's an XML now that it's saved
13:31It's starting to actually highlight it and let me know exactly what's what's in my green is letting me know
13:37This is commented out and my C data is commented and so on so forth that makes it more visually pleasing
13:43and that really covers all the
13:46Extras that we wanted to talk about quickly before we actually start our next phase

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!