, , , - ,
, , , , , , , etc.
Every HTML document will contain the same basic structure
Example 4.1
Example 4.1 is a fully functional — though boring — HTML document, which would display a blank white page.
Notice that HTML tags always come in pairs. One to tell the browser, and one to say . What appears between that set of tags is the content of that set. In the example above, the content of the and tags are just HTML comments that a human reader could see when looking at the source code.
Each pair of tags represents one node known as an "element." The node — part of every HTML document (see the first and last lines of the example) — is also known as the root node which is the first node in the "document." All other nodes spring forth from this root node.
The example above consists of three element nodes: an node, a node and a node.
Take note that in our standard HTML document, all the tags are properly "nested." This means that if a node overlaps another node it is always completely contained within that node. You will never see a proper HTML document with a structure like this:
Example 4.2
The tags in Example 4.2 are not properly nested.
In Example 4.1, notice the node and node are both wholly contained within the node (the and tags start before and close after both and have closed). The and nodes do not intersect with one another.
Programmers have jargon to describe the relationships between these nodes:
Parent: is the parent of and
Child: and are the children of
Sibling: and are siblings.
Grandchild: This example doesn't have any grandchildren, but if we were to add another node (a link for example), to either or , that node would be the grandchild of .
A node can have an infinite number of child nodes, but only ever comes from one parent. Most pages are much more complicated than a blank white page. It is not uncommon for an HTML document to contain dozens or perhaps hundreds of element nodes.
This network of relationships between nodes in an HTML document is the DOM.
===== Why is the DOM important to you? =====
The DOM is how you give your computer directions to find a particular piece of information in an HTML document. Computers can't jump to a desired element node the way our eyes can jump to a particular point on the screen. They need to crawl to that node, visiting all the connected parent nodes along the way, starting at the root.
Here is a simple Table in an HTML document on which to practice.
Example 4.3
|
Precipitation
|
Temperature
|
|
Rain
|
Cool
|
Note: On a well—written web page, you can get some clues as to what is a child of what by looking at the indentation of the lines of code. For each indent, you have undergone another branching of the tree and therefore have a new child node. Unfortunately, not everyone writes clean, easy to read HTML like this. If the website you are translating does not, don't worry, there's a way to work around it that you'll learn about in the next chapter.
Now there are 11 nodes.
Example 4.4
*
*
*
*
*
*
*
* |
* |
* |
* |
Note: | stands for "Table Row" and for "Table Data." This is standard HTML.
Your browser displays this HTML document as
Example 4.5
Precipitation Temperature
Rain Cool
If you wanted to tell someone where to find "Cool" in this table, you would probably say, "Look in the bottom right corner."
To give our computer instructions to find "Cool" you have to tell it where to find that particular node.
Example 4.6
→ → → → 2nd → 2nd |
If you want to visualize this, a tree structure is perhaps easiest to understand.
DOM tree
Fig 4.1: The DOM as a Tree
In plain English, you're looking for the second table data, contained in the second table row, of the table body, of the table, which is part of the body, which in turn is part of the document.
Note: Notice that "Cool" is not the 4th | node. Rather, it is the 2nd | of the 2nd | . This distinction will become important later (and will be addressed in Chapter 5).
==== A quick note on aunts and uncles ====
We have already learned a little family jargon to help us understand the DOM. We know that is the grandparent of . We know that and are siblings. But, not all nodes are related. In the DOM, aunts and uncles count for nothing.
Aunts & Uncles
Fig 4.2: Aunts & Uncles in the DOM
These two nodes are essentially unrelated. You cannot tell the computer to get the "Cool" cell of the table by traveling through the first | node. A legitimate path to "Cool" can only contain the first node if it also contains the second — the parent of "Cool."
For Example:
DOM tree 3
Fig 4.3: DOM Tree w. Multiple Results
Example 4.7
→ → → → → 2nd |
Notice we have simply been less specific about what we were looking for (removed the 2nd in front of | ), but we now have created a path that directs us to both "Temperature" and "Cool." This may or may not be desirable as you will see in Chapter 5.
Nodes need not be tables. Any HTML element is a node. Only certain kinds of nodes contain information that is displayed on web pages:
A headline tag. Generally appears as big and bold (though that can be changed. There are also smaller renditions for less important headlines: , etc.
Paragraph tag. Usually contains text, but can also include images and links.
Image tag. A link to the image, which your browser finds so that it can display the actual image
Link tag. Allows designers to embed a link in a word or series of words
- List item tag. Appears as an item or series of items, often accompanied by bullets.
Table data tag. The content found in a table like the one we practiced on earlier.
The rest of the nodes serve other purposes, often related to how or where a displayed node will appear on the page. Some examples are:
Division tag. Used to separate code into manageable chunks that can be moved around and formatted in a certain way.
Span tag. Used to change the look of smaller pieces of code than would be used for. A few words, for example.
Unordered List. Used in conjunction with a - tag.
Table tag. Defines the opening of a table.
Table row tag. Defines a new line in a table.
===== Practice =====
If you feel confident that you understand the DOM and how to find various element nodes in it, you can skip ahead to the next section. If you would like some more practice, here is a sample HTML document and some questions through which to work.
Example 4.8
| Day |
Month |
| Wednesday |
September |
NiCHE Homepage
| Title |
Author |
Place |
Publisher |
 |
| NiCHE Homepage |
Adam Crymble |
London, ON |
NiCHE |
2009 |
| Zotero |
 |
 |
NiCHE |
Copyright Crymble |
|
1. How many element nodes are there in this example?
2. How many children does the node have?
3. How many great—great—grandchildren does the node have?
4. Describe a path to find the link to the Home Page
5. Describe the path to tell the computer how to find the element node containing the text "Adam Crymble"
6. Describe one path to find "Day" and "Wednesday."
7. Describe one path that will lead to all the data contained in the 2nd .
8. Describe a path that will lead to all the images contained in the 2nd .
9. Describe a path that will lead to only the 2nd and 3rd images in the .
View Answers
===== What you should understand before moving on =====
* At this point, you should understand the fundamentals of HTML documents and the HTML tags that make up those documents.
* Even if you cannot read the source code of a complicated website, you should understand the basic components, including , , and as well as the common HTML elements used to output information to users: tables, text, images and links.
* You should understand nodes and the relationships that they have with other nodes within an HTML document.
* You should be able to explain in plain English how to find any element node in a basic HTML document using the DOM.
* You might read in other texts that you should "Access the DOM." For your purposes, you can substitute "Use an XPath" for these words. You'll learn how to do this in Chapter 5 and Chapter 11.
===== Further Reading =====
* W3Schools HTML tutorial
* W3Schools HTML DOM tutorial
* W3Schools JS DOM tutorial
**Next**: [[dev/How to Write a Zotero Translator, 2nd Edition/Chapter 5|Chapter 5: XPaths (Directions)]] | | |