运行以下简单脚本:
from lxml import etree
tree = etree.parse('VimKeys.xml')
root = tree.getroot()
for child in root:
print ("<table>")
print ("<caption>" + child.attrib['title'] + "</caption>")
for child in child:
print ("<tr>")
print ("<th>" + child.text + "</th>")
print ("<td>" + child.attrib['description'] + "</td>")
print ("</tr>")
print ("</table>")
针对以下xml:
<keycommands>
<category title="Editing">
<key description="replace">r</key>
<key description="change, line">c,cc</key>
<key description="join line with the following">J</key>
<key description="delete & insert">s</key>
<key description="change case">~</key>
<key description="apply last edit">.</key>
<key description="undo, redo">u,⌃+r</key>
<key description="indent line right, left">>>,<<</key>
<key description="auto-indent line">==</key>
</category>
</keycommands>
结果如下:
<caption>Editing</caption>
<tr>
<th>r</th>
<td>replace</td>
</tr>
<tr>
<th>c,cc</th>
<td>change, line</td>
</tr>
<tr>
<th>J</th>
<td>join line with the following</td>
</tr>
<tr>
<th>s</th>
<td>delete & insert</td>
</tr>
<tr>
<th>~</th>
<td>change case</td>
</tr>
<tr>
<th>.</th>
<td>apply last edit</td>
</tr>
<tr>
<th>u,⌃+r</th>
<td>undo, redo</td>
</tr>
<tr>
<th>>>,<<</th>
<td>indent line right, left</td>
</tr>
<tr>
<th>==</th>
<td>auto-indent line</td>
</tr>
</table>
这是无效的 HTML,因为小于和大于号被引用为
< and >
在源文档中。
我怎样才能将这些保存在最终产品中?