11.11. Changing Data Content in a DOM Tree

We have already seen how the DOM allows a tree structure to be rearranged. The content of text nodes can also be changed. In the program below, any telephone number matching the pattern “096 [number]” is changed to “+353 96 [number]”. This task is made easy by the regular-expression-based substitution capabilities provided by Python's re module. The sub method replaces any regular expression matches with the specified string. The string can contain references to groups in the regular expression. The string "\1" refers to the first group, "\2" refers to the second group and so on. In the example below, the pattern "096 (\d+)" is used to locate telephone numbers that match the required pattern. Note the ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.