找不到任何東西準備好, 但它應該或多或少地直接使用ElementTree(或甚至另一個xml庫 - 它只是我更熟悉ElementTree)。
的片段波紋管似乎XPath的有限子集是需要爲它工作:
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as ET
def build_xpath(node, path):
components = path.split("/")
if components[0] == node.tag:
components.pop(0)
while components:
# take in account positional indexes in the form /path/para[3] or /path/para[location()=3]
if "[" in components[0]:
component, trail = components[0].split("[",1)
target_index = int(trail.split("=")[-1].strip("]"))
else:
component = components[0]
target_index = 0
components.pop(0)
found_index = -1
for child in node.getchildren():
if child.tag == component:
found_index += 1
if found_index == target_index:
node = child
break
else:
for i in range(target_index - found_index):
new_node = ET.Element(component)
node.append(new_node)
node = new_node
if __name__ == "__main__":
#Example
root = ET.Element("root")
build_xpath(root, "root/foo/bar[position()=4]/snafu")
print ET.tostring(root)
乍一看,這似乎是不可能的創建基於XPath的「_stricto sensu_」這樣的元素。例如,它會爲'// foo/bar'生成什麼? OTOH,它似乎有可能基於XPath的一個子集生成XML - 事實上它似乎是一個好主意。 – brandizzi 2011-04-14 15:22:04