我是 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>
我非常感謝任何幫助。
丹尼斯
不幸的是,我無法解釋更多,但我必須在我的帖子中添加更多細節,然后是代碼。所以我必須寫更多的詞,即使我不希望這只是為了填補這個空間。哇,我必須寫很多額外的東西來提交這個問題。很抱歉,但我的代碼示例只要它是。再次抱歉。
uj5u.com熱心網友回復:
使用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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471899.html
