使用Python进行XML排序



我是Python的新手,我需要一些帮助。我尝试通过使用LXML按"描述"对"规则"-标签进行排序来重新排列xml。我可以使用以下命令来处理DescriptionTags:

for elem in root.iter('{http://www.ProgramConfiguration/2.1}Description'):
print(elem.text)

但是我不能用它来排序。

我想对它进行变换:

<?xml version="1.0" encoding="utf-8"?>
<ProgramConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ProgramConfiguration/2.1">
<Rules>
<Rule RuleId="1" Enabled="true">
<Description>Muster, Alex</Description>.
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="2" Enabled="true">
<Description>Albert, Peter</Description>
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="3" Enabled="true">
<Description>Rich, Sam</Description>
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="4" Enabled="true">
<Description>Albert, Zack</Description>
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
</Rules>
</ProgramConfiguration>

这:

<?xml version="1.0" encoding="utf-8"?>
<ProgramConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ProgramConfiguration/2.1">
<Rules>
<Rule RuleId="2" Enabled="true">
<Description>Albert, Peter</Description>
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="4" Enabled="true">
<Description>Albert, Zack</Description>
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="1" Enabled="true">
<Description>Muster, Alex</Description>
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="3" Enabled="true">
<Description>Rich, Sam</Description>
<WatchDirectories>
<WatchDirectory Path="\server201...." WatchSubDirs="false" />
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
</Rules>
</ProgramConfiguration>

我非常感谢你的帮助。

丹尼斯

不幸的是,我不能解释更多,但我必须添加一些更多的细节到我的帖子然后代码。所以我必须多写一些字,即使我不想让这个只填补这个的空白。哇,我必须写很多额外的东西来提交这个问题。对不起,但我的代码示例是只要它是。再次道歉。

使用lxml包按元素文本排序

from lxml import etree
from io import BytesIO
xml_obj = BytesIO(xmlstr)
root = etree.parse(xml_obj).getroot()
# keys list before sorting
print(root.xpath('.//x:Rule/x:Description/text()', namespaces={'x': 'http://www.ProgramConfiguration/2.1'}))
for c in root.xpath('/x:ProgramConfiguration/x:Rules', namespaces={'x': 'http://www.ProgramConfiguration/2.1'}):
c[:] = sorted(c, key=lambda child: (child.xpath('.//x:Description/text()', namespaces={'x': 'http://www.ProgramConfiguration/2.1'})))
# keys list after sorting
print(root.xpath('.//x:Rule/x:Description/text()', namespaces={'x': 'http://www.ProgramConfiguration/2.1'}))
xmlstr = etree.tostring(root, encoding="utf-8", method="xml")
print(xmlstr.decode("utf-8"))

结果:

['Muster, Alex', 'Albert, Peter', 'Rich, Sam', 'Albert, Zack']
['Albert, Peter', 'Albert, Zack', 'Muster, Alex', 'Rich, Sam']
<ProgramConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ProgramConfiguration/2.1">
<Rules>
<Rule RuleId="2" Enabled="true">
<Description>Albert, Peter</Description>
<WatchDirectories>
<WatchDirectory Path="server201...." WatchSubDirs="false"/>
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="4" Enabled="true">
<Description>Albert, Zack</Description>
<WatchDirectories>
<WatchDirectory Path="server201...." WatchSubDirs="false"/>
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="1" Enabled="true">
<Description>Muster, Alex</Description>.
<WatchDirectories>
<WatchDirectory Path="server201...." WatchSubDirs="false"/>
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
<Rule RuleId="3" Enabled="true">
<Description>Rich, Sam</Description>
<WatchDirectories>
<WatchDirectory Path="server201...." WatchSubDirs="false"/>
</WatchDirectories>
<Actions>
.
.
.
</Actions>
</Rule>
</Rules>
</ProgramConfiguration>

最新更新