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

XML Elements

The first step in learning about how information is structured in an XML document (the word “document” will be used to refer to information and its organization, typically stored in a disk file) will be to learn the syntactic components. The most basic component in XML is called an “element” or “node”: both are used inter-changeably in documentation.

An element consists of a “name” assigned to it, as well as the content which the name describes/defines. One can also use the word “tag” to describe a name, in conformance with HTML terminology. As it turns out, “tag” is popular universally, and in this document, we shall stick to this terminology. Each element has an opening tag, content (optional) and a closing tag. Sometimes, a single tag might be enough to describe an element, especially when no meaningful content can be stored between the opening and closing of a tag. Learning by example is the best way, so we will see a few below.

First let us look at what an element typically looks like:

<name>Babe Ruth</name>

The parts of this element are as follows:

• <name> is the opening tag, and “name” is the tag-name

• “Babe Ruth” is the content of the element, and here it is a character string

• </name> is the closing tag: note the forward-slash “/” at the beginning

Another example of an element is: <drinking minor=”yes” />

• Note here that there is only one tag, which both opens and closes the element; the tag name is “drinking”

• “minor” is called an “attribute” or “property” of the element. It has the value “yes” in this case.

XML, unlike HTML, is a case sensitive markup language. The content of an element can be a character string, an alphanumeric string, a number etc.; it can be any type of information that is normally used by people and applications. When it comes to tag names, the following exclusions apply:

• The word “XML” or “xml” cannot be used as a tag name… it is the ONLY reserved keyword in an XML document

• A tag name or attribute name cannot start with a numeric character; for example, “32age” is not a valid identifier (another name for tag or attribute name,) whereas “age32” is valid.

• You can't have a space character within an identifier name: for example, “player name” is not a valid identifier, while “player_name” is.

This ends the introduction to syntax and naming of tags and attribute names.

Video Transcript

00:00Alrighty so we got that basic intro and overview of XML and I think it's time
00:06for us to actually jump in. Before we even learn about the structure and
00:10actually the because XML is a structural language but what I want to do I want to
00:15start with an element I want you to first see what it is and then we'll
00:18take a step back and actually see why we actually created in this way. So the
00:23first thing we need to learn is we sent that data contrary to the historical
00:27part of data in HTML which it wasn't really accessible. There was no easy
00:31way for me to say hey I want that data fetch me only that data. XML is whole
00:37structure is designed to try to help you retrieve data in the most efficient way
00:42possible. Now we're gonna see later that that most efficient way possible is
00:46actually a tree structure but before we even look into that tree structure and
00:50see basically how tree structures look like and why do they look the way that
00:54they look like we really want to see how to create one node and I have to say at
00:59this point you're gonna hear in different places you're gonna read
01:01online sometimes people are gonna call whatever we're gonna do right now or
01:05they're gonna call it a node or they're gonna call it an element. It's exact same
01:09thing don't worry about those technicalities. So let's see how we
01:12create a node. Basically to create a node it's very simple we open and close
01:16the less than and greater than and we create basically this little bucket you
01:21can see here here is my little bucket. So this is my bucket. Now I could put
01:27inside of this bucket whatever I want. Now it's an important thing to note is that
01:31every bucket has a name but let's call it in the name of what it is so I'm
01:35gonna throw around once in a while I'll call it an element once in a while
01:39I'll call it a node just to make you guys more and girls more comfortable
01:43with the terminologies. So I'm taking this node and I decided that I'm gonna
01:47give this nodes name I'm gonna call it bucket. Why did I call it bucket? Well
01:52later on I might be able more easily to access it now that I know what it's
01:56called. Now we don't really have anything in this bucket it's completely empty so
02:01the next step for us is actually to put some sort of content inside. Now before
02:05we put any content inside it's really important to notice that even though
02:09we have a bucket and we really could name it any with anything we want to
02:13name it there are a few things we're not allowed to do. We're not allowed to call
02:16it XML even though if you do do it no one's gonna punish you and actually
02:21technically nothing wrong will happen but XML is really the only reserved word
02:27in XML all the rest is fair game so you can't use the word XML but what you
02:32could do is name any other name that starts with the digits so if I want to
02:37call it node I'm sorry not a digit but a character I'm allowed to use
02:40digits but they have to be after I use at least one character so I can't start
02:45calling a node by just calling it 32 Ben that's illegal all right you might
02:51build an application it might work but you're not allowed to do it that way.
02:54Now XMLs are also case sensitive and you're not allowed to have spaces in
02:59them so I can't call a node Ben Fala with a space if I really wanted to call
03:04a node Ben Fala I could use an uppercase F and then just create here
03:08or if I'm really comfortable calling it all lowercase and all lowercase but an
03:12important thing to note is whatever strategy you decide to use be consistent
03:17don't once in a while change it so some people prefer using a camel case I
03:21personally prefer using camel cases some people you prefer using an
03:25underscore so if you want to use an underscore for space then use an
03:29underscore for space all right so that's basically our naming structure
03:33rules and just just repeating it one more time our name can't be XML we
03:39can't start with a number but we could use numbers so it's really for
03:43characters or numbers in English and we want to start with a character always
03:47such as Ben or such as name and then we could use digits if we wanted to I
03:52personally don't want to use any digits and I personally even recommend usually
03:57don't use digits unless it really really makes sense and you'll see later
04:01as you get more used to XML when you get more comfortable then you'll be able
04:05to make those decisions alright so we covered the name part then the next
04:09part like we said an element a node is really a bucket now as a bucket it needs
04:15to be able to contain information and because it needs to be able to contain
04:18information we need to be able to say from here until here is this bucket
04:25called name or this bucket called be an HTML be would make something bold so if
04:32I wanted to make this copy bold I would say I want to take all this copy from
04:36from this point from the beginning from where B begins all the way until the
04:41end and until here I want it to be bold and notice what I did I added this
04:45new slash with my B basically my opening brat my opening node my node
04:51name started with just a name and opening and closing bracket and then in
04:58our closing statement we created the exact same thing only put here a slash
05:01letting us know that this is a closing name now if we put another B I would
05:05think that we're opening another bold tag but we're not we actually opened and
05:10we close it so that was an important factor alright so we have your our
05:15name and we have it and and and we see that everything and this is the next
05:20rule everything that opens must close so if I opened up a bracket I must close
05:25it somewhere even if it's completely empty then I would just leave it
05:29completely empty but a bucket is a bucket you have to say where it starts
05:32and where it ends because if not whatever parts or whatever render just
05:36wouldn't know how to deal with it and it's illegal alright so we created we
05:41now know how to create a node the next step after this is just seeing how
05:45could you create a shorthand node some nodes just never have any information
05:49them for example in HTML there is a node that is called or an element that it's
05:53called br what does br do br enables you to break a line in HTML by just
05:59putting that lot just putting this bracket just putting this symbol all
06:03this stuff together it would just break a line now until HTML 4 don't really
06:07care if you close close things or not the browser's responsibility was to
06:11figure it out but since HTML 4 and as our standard as a XML we always have to
06:17close everything and the way we do that we put a space after the name kind of
06:21indicating that we're done with the name and then we put a slash just the same
06:25slash as we did for closing earlier and this indicates that this is a self
06:29contained bucket that has not no content and again the name could have
06:34been any name we wanted wanted to we're just using br because that's the
06:38way you probably be more familiar with if you've worked with HTML now by the
06:44way just one more note about this is HTML is case sensitive so you want to
06:50make sure that you don't call your br uppercase and then another place
06:54lowercase HTML would be forgiving about that XML is whatever you type that's
06:59whatever you get that's it's really important to be consistent so I would
07:03recommend always use lowercase and only use an uppercase whenever you're
07:07starting a new word but be consistent with it okay so let's just recap so we
07:12saw if I just delete this we saw if we create a new node and let's say we're
07:15gonna call this node bucket not bucket 3 and we want to close that bucket
07:22immediately and I'm gonna put some content into this bucket so this bucket
07:27is gonna have one inside of it and that's completely legal we've just
07:31created a bucket and the information that that bucket contains is the value
07:35one this is the content of our bucket now we've learned also that we could
07:42create buckets that are empty such as the br element and by just all we have to
07:48do is put a space and then a slash and we close our bracket and then there's
07:54no continuation we can't put here any content this content is not part of our
07:58bucket it's out of the bucket it's illegal it's not part of anything all
08:03right and that covers it that's all you really need to know to know what an
08:07element is or what a node is and our next step is going to see how do we
08:13actually combine this into a structure that will make it a little bit more
08:16useful so we're kind of taking steps back until this is going to start
08:19becoming useful

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!