XML 使用 Python 排序

XML sorting with Python

我是 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>

非常感谢任何帮助。

丹尼斯

不幸的是,我无法解释更多,但我必须向我的 post 然后代码添加更多详细信息。所以我不得不多写一些话,即使我不希望这个只填space。哇,我必须写很多额外的东西来提交这个问题。很抱歉,但我的代码示例就是这样。再次抱歉。

使用 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>