1

这是xml示例

<?xml version="1.0" encoding="UTF-8"?>
  <BxfMessage id="urn:uuid:dd0a5a80-4217-a0df-a1f0-d62045e45f8a" dateTime="2017-08-04T09:00:00Z" xmlns="http://smpte-ra.org/schemas/2021/2008/BXF" xmlns:pmcp="http://www.atsc.org/XMLSchemas/pmcp/2007/3.1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" messageType="Information" origin="Traffic System" originType="Traffic" userName="PlaylistScript" destination="Automation">
  <BxfData action="add">
  <Schedule action="add" type="Primary" scheduleId="urn:uuid:12234-1242-afef1-1341541" dayPattern="1000000" scheduleStart="2017-08-04T09:00:00Z" scheduleEnd="2017-08-05T09:00:00Z">
    <Channel channelNumber="1" status="active" type="digital_television" ca="false" shortName="LASER">
      <pmcp:Name lang="eng">LASER</pmcp:Name>
    </Channel>
    <ScheduleName>LASER_170804</ScheduleName>
    <!-- rev022 -->
     <ScheduledEvent>
       <ScheduleElements>
         <EventData eventType="Primary-ProgramHeader" action="add">
           <EventId>
             <EventId>urn:uuid:102F29C1-999F-4D6C-AF36-1235363AD</EventId>
           </EventId>
           <EventTitle>PAID PROGRAMMING</EventTitle>
           <PrimaryEvent>
             <ProgramEvent>
               <SegmentNumber>0</SegmentNumber>
               <ProgramName>PAID PROGRAMMING</ProgramName>
             </ProgramEvent>
           </PrimaryEvent>
           <StartDateTime>
             <SmpteDateTime broadcastDate="2017-08-04">
               <SmpteTimeCode>09:00:00:00</SmpteTimeCode>
              </SmpteDateTime>
           </StartDateTime>
           <LengthOption>
             <Duration>
               <SmpteDuration>
                 <SmpteTimeCode>00:30:00:00</SmpteTimeCode>
               </SmpteDuration>
             </Duration>
           </LengthOption>
           <EndMode>Duration</EndMode>
         </EventData>
       </ScheduleElements>
     </ScheduledEvent>
   </Schedule>
  </BxfData>
</BxfMessage>

这是我用来尝试从 element.tag 字符串中剥离命名空间的 Python 代码

    tree = etree.parse("file.xml")
    root = tree .getroot()
    objectify.deannotate(root, cleanup_namespaces=True, xsi=True, pytype=True)  # supposed to remove namespaces

    for element in root.iter():
        print("%s - %s" % (element.tag, element.text))

所以我有这个函数,我需要从我正在解析的这个 xml 文档中删除命名空间。当我遍历 xml 树时, deannotate 函数仍然将它们留在那里。PyCharm 调试器

示例输出只是元素名称,而不是前面的命名空间

所以元素只是“BxfMessage”,而不是“ http://smpte-ra.org/schemas/2021/2008/BXF BxfMessage”

我使用的是 lxml 4.0 版。我尝试启用所有选项。我不确定我在这里做错了什么。

4

0 回答 0