| 1 | module xml |
| 2 | |
| 3 | // get_element_by_id returns the first element with the given id, or none if no |
| 4 | // such element exists in the subtree rooted at this node. |
| 5 | pub fn (node XMLNode) get_element_by_id(id string) ?XMLNode { |
| 6 | // Is this the node we're looking for? |
| 7 | if attribute_id := node.attributes['id'] { |
| 8 | if attribute_id == id { |
| 9 | return node |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | if node.children.len == 0 { |
| 14 | return none |
| 15 | } |
| 16 | |
| 17 | // Recurse into children |
| 18 | for child in node.children { |
| 19 | match child { |
| 20 | XMLNode { |
| 21 | if result := child.get_element_by_id(id) { |
| 22 | return result |
| 23 | } |
| 24 | } |
| 25 | else {} |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | return none |
| 30 | } |
| 31 | |
| 32 | // get_elements_by_tag returns all elements with the given tag name in the subtree |
| 33 | // rooted at this node. If there are no such elements, an empty array is returned. |
| 34 | pub fn (node XMLNode) get_elements_by_tag(tag string) []XMLNode { |
| 35 | mut result := []XMLNode{} |
| 36 | |
| 37 | if node.name == tag { |
| 38 | result << node |
| 39 | } |
| 40 | |
| 41 | if node.children.len == 0 { |
| 42 | return result |
| 43 | } |
| 44 | |
| 45 | // Recurse into children |
| 46 | for child in node.children { |
| 47 | if child is XMLNode { |
| 48 | result << child.get_elements_by_tag(tag) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return result |
| 53 | } |
| 54 | |
| 55 | // get_elements_by_attribute returns all elements with the given attribute-value pair in |
| 56 | // the subtree rooted at this node. If there are no such elements, an empty array is returned. |
| 57 | pub fn (node XMLNode) get_elements_by_attribute(attribute string, value string) []XMLNode { |
| 58 | mut result := []XMLNode{} |
| 59 | |
| 60 | if attribute_value := node.attributes[attribute] { |
| 61 | if attribute_value == value { |
| 62 | result << node |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if node.children.len == 0 { |
| 67 | return result |
| 68 | } |
| 69 | |
| 70 | // Recurse into children |
| 71 | for child in node.children { |
| 72 | if child is XMLNode { |
| 73 | result << child.get_elements_by_attribute(attribute, value) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return result |
| 78 | } |
| 79 | |
| 80 | // get_element_by_id returns the first element with the given id, or none if no |
| 81 | // such element exists. |
| 82 | pub fn (doc XMLDocument) get_element_by_id(id string) ?XMLNode { |
| 83 | return doc.root.get_element_by_id(id) |
| 84 | } |
| 85 | |
| 86 | // get_elements_by_attribute returns all elements with the given attribute-value pair. |
| 87 | // If there are no such elements, an empty array is returned. |
| 88 | pub fn (doc XMLDocument) get_elements_by_attribute(attribute string, value string) []XMLNode { |
| 89 | return doc.root.get_elements_by_attribute(attribute, value) |
| 90 | } |
| 91 | |
| 92 | // get_elements_by_tag returns all elements with the given tag name. |
| 93 | // If there are no such elements, an empty array is returned. |
| 94 | pub fn (doc XMLDocument) get_elements_by_tag(tag string) []XMLNode { |
| 95 | return doc.root.get_elements_by_tag(tag) |
| 96 | } |
| 97 | |