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

Attributes

We were briefly introduced to the concept of element attributes in an earlier section, where an attribute is placed in the start tag of an element. An attribute describes a property of the element, along with a relevant value (generally in the context of an application.) If we observe carefully the logic behind organizing XML document elements as tree structures, we can see a duality between nested elements and element attributes.

For example, consider the monkey-business XML document of the previous topic, describing nested element structures. It can be reorganized to have fewer elements, overall (and thus, a less-deeper rooted tree) to look something like this:

<monkey-business venture-name="Simian Platform" />

<monkey-role title="Production Damager" name="KingKong" responsibility="Stop-erations" subordinates="Jerks" />

<monkey-role title="Deceptionist" name="Dunston" responsibility="Curt-essy" />

</monkey-business>

Of course, the smart reader will easily understand that the same information can be reorganized in several different ways by choosing various combinations of element nesting and attribute identification. The choice is up to the document author what he wants to put where, but there is also a method to the madness, as we shall see below.

One obvious difference between the 2 styles is that using attributes reduces the clutter in the XML document, with far lesser elements and much less nesting involved. The element "monkey-role" has no nested elements in the second example, whereas it had 3 to 4 elements nested inside in the first one. Because of this space conservation benefit, time efficiency and readability are achieved almost every time in all scenarios, why have nested elements at all? After all, isn't it excellence that we are always after?

The choice should, however, be context-specific. Although nesting elements has the above disadvantages, it is in fact preferable in several contexts. Since attributes are embedded within the start tags as properties, in several contexts they cannot be extracted with ease by an application. But the most important determinant is that sometimes, within an outer element, we want to have multiple elements of the same category, such as <monkey-role> in our example. If such an element was never unique, then we cannot use it effectively as an attribute of <monkey-business>! So it clearly depends on what the application's behavior will be, and how to model that behavior in the information.

Just as there are no pre-defined limits on the number of nested elements inside a containing element, there are none on the number of attribute-value pairs in an XML element. Only limits on the system govern these extreme bounds.

Video Transcript

00:00Until now, we worked exclusively with elements,
00:04but we didn't look at all at its counterpart,
00:06which is a very powerful and useful counterpart.
00:10We've seen the structure of XML and really,
00:14we've learned everything we need to know
00:15about how to build structure with XML.
00:18Now, it's really just down to practice and actually making it work and seeing
00:21it how it works with
00:23your specific programming language that you're working.
00:26The last component that we want to look at really from
00:28a perspective of structure in XML is really
00:31the counterpart that breaks the structure where it puts it out of the structure.
00:35Let's say, for example, you wanted to put yourself some note regarding the student,
00:39or let me just get this off of the screen for a second,
00:43and let's reminisce at HTML.
00:48We've seen BRs, which were tags that were empty,
00:50that in HTML, which basically break lines in HTML.
00:54We've seen OTO tags that actually contain data,
00:57such as the B tag,
00:58and we said the B tag would make everything bold inside of it.
01:01Let's say we wanted to underscore just the letter N,
01:06then we could have nest inside of our HTML that copy.
01:10We've seen it in our XML separately,
01:13but we could see this OTO in HTML where everything here,
01:16everything between this opening bracket and
01:19a closing B bracket would then be bolded,
01:22while everything between the U bracket and
01:25the closing U bracket would make it underlined.
01:31Basically, our output would probably be
01:34Ben with a bold Ben and underline in our N.
01:39We've seen those type of elements,
01:41but there is another type in HTML,
01:43such as the link references in HTML,
01:47where our tag itself would then have the copy that would be visible for the user,
01:52such as everything FLA,
01:58or maybe we want to make it even more human readable,
02:00everything flash, but we wanted to set the link,
02:05and setting the link up in HTML,
02:07we basically put a space inside of the area of the name.
02:11We'll put a space, type href,
02:14and then open and close a quotation mark,
02:18and then put inside the link which we wanted to go to.
02:22Everything fla.com, and all of this long structure would then
02:28actually become into a link that would only look like everything flash,
02:32and that's how it would output to the user.
02:34Now, if you don't know HTML,
02:36don't worry about this,
02:37but this is a great introduction to people that have worked with HTML,
02:41which I hope some of you have.
02:43All right. Looking at that,
02:45now taking this example into our XML style of references,
02:49it's basically the exact same thing.
02:52If we go back and just look at our student tag,
02:55let's see how could we reformat all this
02:58and just use attributes instead of using our nodes,
03:03instead of using elements.
03:04What if we wanted our students to just have properties,
03:07but that's how we call them, or attributes,
03:10if we wanted it to have properties or attributes
03:12such as the math and English and sports attributes?
03:15Let's see how that would happen.
03:17Let's delete what we have there,
03:20and we're going to start off a new student.
03:22We're going to put a space.
03:24We're going to call our first attribute math,
03:27and we're going to put an equal sign.
03:29Then one thing that's really important is we have to put a quotation mark,
03:32an opening and a closing one.
03:34You could use a single quotation
03:37as long as you close with a single quotation,
03:39or you could use a double as long as you close with a double.
03:42Our math score is 80.
03:45Again, just as we were talking earlier,
03:47try to be consistent.
03:48If you choose to use one quotation mark, try to stick to that.
03:52If you use a double one, then try to use that consistently.
03:55We had our math and English.
03:58Let's say our English score would be 60, let's say.
04:04Let's say we had one more and it will be Spanish for our case.
04:09Spanish would equal 100 because we have a very smart student.
04:14Now, because our student class now doesn't contain anything,
04:18we could just close it up using our closing element,
04:22or maybe we wanted to put here something such as the student's name.
04:26Maybe in between our opening and closing brackets of our student,
04:30where this is our opening bracket and this is our closing bracket,
04:34we could put here the student name as Benfellow.
04:37What we did here, we completely changed the structure.
04:41We put something new inside.
04:42We didn't have to.
04:44If we want, we could just revisit to what we had earlier,
04:47to have it saying that's basically a one-on-one replica.
04:50If I just put inside our student one,
04:53that was before you could see that our student had a score of math of 40.
04:56This was our structure that we had before.
04:58But now that we take that out,
05:00we'll see that our new student is much more lean.
05:04Now, you might be thinking, well, Ben,
05:05this looks a lot more smaller.
05:08Maybe more palatable makes our file smaller,
05:10and you're absolutely right about all those stuff.
05:12You'll actually find out that you're not always going to find
05:17the most useful to use attributes.
05:19Because what are attributes?
05:21Attributes are less searchable.
05:22They're still searchable,
05:24but they're more condensed.
05:26As such, it's a little bit harder for
05:28some programming languages to actually access the data.
05:32Not only that, let's say right now,
05:33you're using your student and you're only using averages.
05:37What if tomorrow morning your boss comes to you,
05:40or you decide that you want to start
05:41collecting every single score of your student?
05:45In this structure, you kind of have to start over.
05:47While in our other structure,
05:51we could have just said, okay,
05:53we're adding now more math scores.
05:55Maybe instead of that, we're going to break a line here,
05:59and we're going to create maybe a score node.
06:08And in that scores, we're going to put here 80,
06:10giving us the option of putting more than one score.
06:12So let's say you have 81 in his first exam,
06:15but his second exam was much better,
06:18or much worse, much worse.
06:20Let's make it much worse.
06:23All right, so that's really the key here.
06:26While our XML structure enabled us to put in as much data
06:30and grow as we needed to,
06:32the attribute on the other hand,
06:35anytime we need to make a change,
06:36it's going to be a much bigger change.
06:38Now, obviously, if you're a server-side programmer,
06:42a lot of times structure is way more important
06:45than how small your files are,
06:47because it doesn't really matter how big the files are,
06:51it just doesn't matter as much.
06:53Or if you have a desktop application,
06:54who cares how big the XML file is
06:56if it's sitting on your desktop?
06:58Well, if your application is a live application,
07:00imagine you're creating a stock ticker,
07:02and you're trying to get that information
07:04into the user's computer as quick as possible.
07:07Every extra character that you add is a negative.
07:11So really, it depends on the scenario.
07:13I know some people will swear
07:14for only using nodes or elements,
07:17and some will swear only using attributes,
07:19and some will just be more practical
07:22and say it depends on the scenario,
07:24and there's no rules and regulations.
07:26You could put anything that was in an attribute
07:28in a node and vice versa,
07:30so really, it's very, very flexible.
07:33And really, the only difference between the two
07:34is like we were saying,
07:35well, this breaks that structure part,
07:38it makes it harder for you to expand on that tree
07:41that virtual tree that we're saying,
07:42because it's not part of the tree.
07:43This is baked directly into our student node,
07:47while our previous structure, our nested structure,
07:51actually enables us to continue growing the tree,
07:53and the tree is growing and growing.
07:55And don't worry about it if you don't know right now
07:57which one is best for you.
07:58Use both, get used to them, explore them.
08:01And you'll see as you get more comfortable
08:03using XML in your own programming language,
08:05you're gonna figure out which style best suits you.
08:08But it's important for you to know how to work with both.

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!