我有一个相关元素列表的列表,其片段如下所示。因此config
和action
是command
的孩子:
all1= [[<Element '{http://clish.sourceforge.net/XMLSchema}COMMAND' at 0x7ff2ae48b690>, <Element '{http://clish.sourceforge.net/XMLSchema}PARAM' at 0x7ff2ae48b710>, <Element '{http://clish.sourceforge.net/XMLSchema}ACTION' at
0x7ff2ae48b750>, <Element '{http://clish.sourceforge.net/XMLSchema}CONFIG' at
0x7ff2ae48b790>], [<Element '{http://clish.sourceforge.net/XMLSchema}COMMAND' at
0x7ff2ae48b890>, <Element '{http://clish.sourceforge.net/XMLSchema}CONFIG' at
0x7ff2ae48b8d0>, <Element '{http://clish.sourceforge.net/XMLSchema}ACTION' at
0x7ff2ae48b950>], [<Element '{http://clish.sourceforge.net/XMLSchema}COMMAND' at
0x7ff2ae48ba10>, <Element '{http://clish.sourceforge.net/XMLSchema}CONFIG' at
0x7ff2ae48ba50>, <Element '{http://clish.sourceforge.net/XMLSchema}ACTION' at
0x7ff2ae48bad0>], [<Element '{http://clish.sourceforge.net/XMLSchema}COMMAND' at
0x7ff2ae48bb90>, <Element '{http://clish.sourceforge.net/XMLSchema}CONFIG' at
0x7ff2ae48bbd0>, <Element '{http://clish.sourceforge.net/XMLSchema}ACTION' at
0x7ff2ae48bc50>]]
这是由:
生成的commands = root.findall('{http://clish.sourceforge.net/XMLSchema}'
'VIEW/{http://clish.sourceforge.net/XMLSchema}COMMAND')
command_string = ""
all1 = []
for command in commands:
all1.append(list(command.iter()))
all1 = [x for x in all1 if len(x) > 2]
print all1
我希望将每个action元素的文本更改为每个配置标记的属性。
这是我的意思的一个例子:
<COMMAND name="shutdown"
help="Shutdown the selected interface">
<CONFIG priority="0x7F01" />
<ACTION>
/klas/klish-scripts/interfaces.py conf -i ${iface} --enable 0
</ACTION>
</COMMAND>
<COMMAND name="no shutdown"
help="Enable the selected interface">
<CONFIG operation="unset" pattern="^shutdown"/>
<ACTION>
/klas/klish-scripts/interfaces.py conf -i ${iface} --enable 1
</ACTION>
</COMMAND>
第一个配置有一个attrib,所以它的动作标签文本应该更改为“0x7F01” 在第二个配置中有“unset”和“shutdown”,因此应该是新的操作文本,将其保留为新的xml:
<COMMAND name="shutdown"
help="Shutdown the selected interface">
<CONFIG priority="0x7F01" />
<ACTION>
0x7F01
</ACTION>
</COMMAND>
<COMMAND name="no shutdown"
help="Enable the selected interface">
<CONFIG operation="unset" pattern="^shutdown"/>
<ACTION>
unset ^shutdown
</ACTION>
</COMMAND>
所以我应该将每个attrib设置为一个变量并将其传递给action.text?
如果我在创建列表之前完成了这些操作,这会更容易吗?通过操纵元素然后添加它们?我已经引用了标签,文本等。或者我仍然可以使用元素列表来执行此操作吗?