Return the content from the passed xml xpath, or return the result of a passed function (receives xpathContext as its only arg)
(xml, path=None, func=None)
| 76 | |
| 77 | |
| 78 | def get_xml_path(xml, path=None, func=None): |
| 79 | """ |
| 80 | Return the content from the passed xml xpath, or return the result |
| 81 | of a passed function (receives xpathContext as its only arg) |
| 82 | """ |
| 83 | doc = None |
| 84 | ctx = None |
| 85 | result = None |
| 86 | |
| 87 | try: |
| 88 | doc = libxml2.parseDoc(xml) |
| 89 | ctx = doc.xpathNewContext() |
| 90 | |
| 91 | if path: |
| 92 | ret = ctx.xpathEval(path) |
| 93 | if ret is not None: |
| 94 | if type(ret) == list: |
| 95 | if len(ret) >= 1: |
| 96 | result = ret[0].content |
| 97 | else: |
| 98 | result = ret |
| 99 | |
| 100 | elif func: |
| 101 | result = func(ctx) |
| 102 | |
| 103 | else: |
| 104 | raise ValueError("'path' or 'func' is required.") |
| 105 | finally: |
| 106 | if doc: |
| 107 | doc.freeDoc() |
| 108 | if ctx: |
| 109 | ctx.xpathFreeContext() |
| 110 | return result |
| 111 | |
| 112 | |
| 113 | def pretty_mem(val): |
no test coverage detected