Parse xml nodes. This method will parse the children and attributes of elements in ``xpath``, conditionally for only elements, only attributes or both while optionally renaming node names. Raises ------ ValueError * If only eleme
(self, elems: list[Any])
| 195 | raise AbstractMethodError(self) |
| 196 | |
| 197 | def _parse_nodes(self, elems: list[Any]) -> list[dict[str, str | None]]: |
| 198 | """ |
| 199 | Parse xml nodes. |
| 200 | |
| 201 | This method will parse the children and attributes of elements |
| 202 | in ``xpath``, conditionally for only elements, only attributes |
| 203 | or both while optionally renaming node names. |
| 204 | |
| 205 | Raises |
| 206 | ------ |
| 207 | ValueError |
| 208 | * If only elements and only attributes are specified. |
| 209 | |
| 210 | Notes |
| 211 | ----- |
| 212 | Namespace URIs will be removed from return node values. Also, |
| 213 | elements with missing children or attributes compared to siblings |
| 214 | will have optional keys filled with None values. |
| 215 | """ |
| 216 | |
| 217 | dicts: list[dict[str, str | None]] |
| 218 | |
| 219 | if self.elems_only and self.attrs_only: |
| 220 | raise ValueError("Either element or attributes can be parsed not both.") |
| 221 | if self.elems_only: |
| 222 | if self.names: |
| 223 | dicts = [ |
| 224 | { |
| 225 | **( |
| 226 | {el.tag: el.text} |
| 227 | if el.text and not el.text.isspace() |
| 228 | else {} |
| 229 | ), |
| 230 | **{ |
| 231 | nm: ch.text if ch.text else None |
| 232 | for nm, ch in zip(self.names, el.findall("*"), strict=True) |
| 233 | }, |
| 234 | } |
| 235 | for el in elems |
| 236 | ] |
| 237 | else: |
| 238 | dicts = [ |
| 239 | {ch.tag: ch.text if ch.text else None for ch in el.findall("*")} |
| 240 | for el in elems |
| 241 | ] |
| 242 | |
| 243 | elif self.attrs_only: |
| 244 | dicts = [ |
| 245 | {k: v if v else None for k, v in el.attrib.items()} for el in elems |
| 246 | ] |
| 247 | |
| 248 | elif self.names: |
| 249 | dicts = [ |
| 250 | { |
| 251 | **el.attrib, |
| 252 | **({el.tag: el.text} if el.text and not el.text.isspace() else {}), |
| 253 | **{ |
| 254 | nm: ch.text if ch.text else None |