Python lxml не распознает дочерний элемент, хотя дочерний элемент существует согласно getchildren () - PullRequest
0 голосов
/ 04 июня 2019

У меня есть следующий XML:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns7:GetStopMonitoringServiceResponse xmlns:ns3="http://www.siri.org.uk/siri" xmlns:ns4="http://www.ifopt.org.uk/acsb" xmlns:ns5="http://www.ifopt.org.uk/ifopt" xmlns:ns6="http://datex2.eu/schema/1_0/1_0" xmlns:ns7="http://new.webservice.namespace">
            <Answer>
                <ns3:ResponseTimestamp>2019-03-31T09:00:52.912+03:00</ns3:ResponseTimestamp>
                <ns3:ProducerRef>ISR Siri Server (141.10)</ns3:ProducerRef>
                <ns3:ResponseMessageIdentifier>276480603</ns3:ResponseMessageIdentifier>
                <ns3:RequestMessageRef>0100700:1351669188:4684</ns3:RequestMessageRef>
                <ns3:Status>true</ns3:Status>
                <ns3:StopMonitoringDelivery version="IL2.71">
                    <ns3:ResponseTimestamp>2019-03-31T09:00:52.912+03:00</ns3:ResponseTimestamp>
                    <ns3:Status>true</ns3:Status>
                    <ns3:MonitoredStopVisit>
                        <ns3:RecordedAtTime>2019-03-31T09:00:52.000+03:00</ns3:RecordedAtTime>
                        <ns3:ItemIdentifier>-881202701</ns3:ItemIdentifier>
                        <ns3:MonitoringRef>20902</ns3:MonitoringRef>
                        <ns3:MonitoredVehicleJourney>
                            <ns3:LineRef>23925</ns3:LineRef>
                            <ns3:DirectionRef>2</ns3:DirectionRef>
                            <ns3:FramedVehicleJourneyRef>
                                <ns3:DataFrameRef>2019-03-31</ns3:DataFrameRef>
                                <ns3:DatedVehicleJourneyRef>36962685</ns3:DatedVehicleJourneyRef>
                            </ns3:FramedVehicleJourneyRef>
                            <ns3:PublishedLineName>15</ns3:PublishedLineName>
                            <ns3:OperatorRef>15</ns3:OperatorRef>
                            <ns3:DestinationRef>26020</ns3:DestinationRef>
                            <ns3:OriginAimedDepartureTime>2019-03-31T08:35:00.000+03:00</ns3:OriginAimedDepartureTime>
                            <ns3:VehicleLocation>
                                <ns3:Longitude>34.78000259399414</ns3:Longitude>
                                <ns3:Latitude>32.042293548583984</ns3:Latitude>
                            </ns3:VehicleLocation>
                            <ns3:VehicleRef>37629301</ns3:VehicleRef>
                            <ns3:MonitoredCall>
                                <ns3:StopPointRef>20902</ns3:StopPointRef>
                                <ns3:ExpectedArrivalTime>2019-03-31T09:03:00.000+03:00</ns3:ExpectedArrivalTime>
                            </ns3:MonitoredCall>
                        </ns3:MonitoredVehicleJourney>
                    </ns3:MonitoredStopVisit>
                </ns3:StopMonitoringDelivery>
            </Answer>
        </ns7:GetStopMonitoringServiceResponse>
    </S:Body>
</S:Envelope>

Я использую lxml objectify, чтобы преобразовать xml в объект, а затем пытаюсь получить доступ к дочерним элементам, как описано в документах

Это мой код для загрузки:

from lxml import objectify
obj = objectify.fromstring(xml_content)

Пока следующий код работает нормально:

print(obj.Body.tag)

{http://schemas.xmlsoap.org/soap/envelope/}Body

Я получаю сообщение об ошибке при привязке к дочернему элементу Body (GetStopMonitoringServiceResponse):

print(obj.Body.GetStopMonitoringServiceResponse.tag)

AttributeError: no such child: {http://schemas.xmlsoap.org/soap/envelope/}GetStopMonitoringServiceResponse

Но когда я пытаюсь заполучить детей Тела, я вижу этот элемент:

print(obj.Body.getchildren())

[<Element {http://new.webservice.namespace}GetStopMonitoringServiceResponse at 0x1d6e17f0908>]

Что мне здесь не хватает?

1 Ответ

0 голосов
/ 04 июня 2019

Чтобы получить доступ к элементу, который связан с пространством имен, отличным от его родителя, вы можете использовать getattr():

g = getattr(obj.Body, "{http://new.webservice.namespace}GetStopMonitoringServiceResponse")
print(g.tag)

Работает также следующее:

g = obj.Body["{http://new.webservice.namespace}GetStopMonitoringServiceResponse"]
print(g.tag)

Ссылка: https://lxml.de/objectify.html#namespace-handling.

...