Methods for Working with DOM
Sequences
Sometimes it is required to divide the sequence of nodes into logical blocks using the header (the leading node of the logical block) or the footer (the final node of the logical block). For this operation you can use the sequence command. This command, unlike a split command, works with the nodes of the current block, so as parameters you will have to pass CSS selectors.
Following parameters can be used with the command:
Parameter | Description |
---|---|
selector | Selectors for nodes that must be selected in the current block. Bear in mind that the nodes, used as a header or a footer, should also be selected by selectors in this parameter. |
header | A selector for nodes that will be used as header of logical blocks. If you are using this parameter, the footer parameter will be ignored if it is also passed. |
footer | A selector for nodes that will be used as footer of logical blocks. If you are passing also the header parameter, this parameter will be ignored. |
Let's use following HTML source for our examples:
<div>
<h1>Some title</h1>
<p>Some text 1</p>
<p>Some text 1 - 1</p>
<p class="footer">My footer</p>
<h1>Some other title</h1>
<p>Some text 2</p>
<p class="footer">My other footer</p>
<h1>Another title</h1>
<p>Some text 2</p>
<p>Final p</p>
<p class="footer">My last footer</p>
</div>
As you can see, we can take the tag h1 as a header node for splitting into additional blocks in this example, or p tag with the footer class as a footer.
Now, let's try to split the common div block into a set of separate, independent blocks.
Let's take a closer look at the examples:
- find:
path: div
do:
- sequence:
selector: p,h1
header: h1
# WE WILL GET NEW BLOCK CREATED BY COMMAND `sequence`
# WITH FOLLOWING HTML CONTENTS:
# <div class="sequence element_0">
# <h1>Some title</h1>
# <p>Some text 1</p>
# <p>Some text 1 - 1</p>
# <p class="footer">My footer</p>
# </div>
# <div class="sequence element_1">
# <h1>Some other title</h1>
# <p>Some text 2</p>
# <p class="footer">My other footer</p>
# </div>
# <div class="sequence element_2">
# <h1>Another title</h1>
# <p>Some text 2</p>
# <p>Final p</p>
# <p class="footer">My last footer</p>
# </div>
# LETS EXTRACT HEADER OF LAST ELEMENT OF SPLITTED BLOCKS
- find:
path: .sequence > h1
slice: -1
do:
- parse
# REGISTER VALUE: Another title
In the next chapter, we will learn how to create a new block from the contents of the register.