添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
我试图通过使用LXML将 "规则 "标签按 "描述 "分类来重新排列一个xml。 我可以用以下方法来处理描述标签。

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>

Into this:

<?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>

我非常感谢任何帮助。

Dennis

不幸的是,我无法解释更多,但我必须在我的帖子中增加一些细节,然后是代码。所以我必须多写一些字,即使我不想这样做只是为了填补这个空间。哇,我必须写很多额外的东西来提交这个问题。很抱歉,但我的代码例子已经够长了,所以再次抱歉。

3 个评论
谢谢,这是我之前发现的。它是使用属性。我喜欢用{来排序程序配置/2.1}Description.text
LMC
下面补充了我的答案。
python
xml
Dennis
Dennis
发布于 2022-05-04
1 个回答
LMC
LMC
发布于 2022-05-04
已采纳
0 人赞同

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

Result:

['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>