Nokogiri 中的 XPATH

XPATH in Nokogiri

我有一个带有 Nokogiri 的 XML + SOAP,我只想显示正文,但我有一些错误。 XML SOAP 是:

data = <SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">
        <SOAP:Header><header xmlns=\"xmlapi_1.0\">
            <requestID>aquiroga:0001</requestID>
            <requestTime>Mar 14, 2018 11:33:27 PM</requestTime>
            <responseTime>Mar 14, 2018 11:33:27 PM</responseTime>
        </header>
    </SOAP:Header>
    <SOAP:Body>
        <findResponse xmlns=\"xmlapi_1.0\">
            <result>
                <service.SapEgrQosQueueStatsLogRecord>
                    <queueId>2</queueId>
                    <droppedInProfOctets>0</droppedInProfOctets>
                    <droppedOutProfOctets>0</droppedOutProfOctets>
                    <timeCaptured>1521072355115</timeCaptured>
                    <periodicTime>0</periodicTime>
                    <displayedName>eNodo_B-HOTELPUERTOPALMERA-S1UX2</displayedName>
                    <monitoredObjectSiteName>SMT-TRS-AN01</monitoredObjectSiteName>
                </service.SapEgrQosQueueStatsLogRecord>
                <service.SapEgrQosQueueStatsLogRecord>
                    <queueId>1</queueId>
                    <droppedInProfOctets>0</droppedInProfOctets>
                    <droppedOutProfOctets>70450698</droppedOutProfOctets>
                    <timeCaptured>1521072355051</timeCaptured>
                    <periodicTime>199796</periodicTime>
                    <displayedName>to_eNODOB Tarapoto Sur-S1-MME</displayedName>
                    <monitoredObjectSiteName>SMT-TRS-AN01</monitoredObjectSiteName>
                </service.SapEgrQosQueueStatsLogRecord>
            </result>
        </findResponse>
    </SOAP:Body>
    </SOAP:Envelope>

我使用的代码是:

data_stats = data.xpath('/SOAP:Envelope/SOAP:Body')

结果是:

=> #<Nokogiri::XML::Element:0x3fee745c1c94 name="Body" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1e60 prefix="SOAP" href="http://schemas.xmlsoap.org/soap/envelope/"> children=[#<Nokogiri::XML::Element:0x3fee745c16b8 name="findResponse" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Element:0x3fee745c126c name="result" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Element:0x3fee745c0e84 name="service.SapEgrQosQueueStatsLogRecord" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Element:0x3fee745c0a60 name="queueId" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Text:0x3fee745c0664 "1">]>, #<Nokogiri::XML::Element:0x3fee745c0498 name="droppedInProfOctets" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Text:0x3fee745c00b0 "0">]>, #<Nokogiri::XML::Element:0x3fee745adec4 name="droppedOutProfOctets" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Text:0x3fee745adaa0 "0">]>, #<Nokogiri::XML::Element:0x3fee745ad8d4 name="timeCaptured" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Text:0x3fee745ad4c4 "1521158716279">]>, #<Nokogiri::XML::Element:0x3fee745ad2f8 name="periodicTime" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Text:0x3fee745aceac "284992">]>, #<Nokogiri::XML::Element:0x3fee745acce0 name="displayedName" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Text:0x3fee745ac8bc "RTN VillaSalvador3 Tdp-VillaBaja">]>, #<Nokogiri::XML::Element:0x3fee745ac6dc name="monitoredObjectSiteName" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0"> children=[#<Nokogiri::XML::Text:0x3fee745ac2b8 "LIM-SPO-AN02">]>]>, #<Nokogiri::XML::Element:0x3fee74593f9c name="service.SapEgrQosQueueStatsLogRecord" namespace=#<Nokogiri::XML::Namespace:0x3fee745c1668 href="xmlapi_1.0">

我希望它仅在 service.SapEgrQosQueueStatsLogRecord 中出现。我使用的代码如下:

data_stats = qos7705egressdiscard_summary.xpath('/SOAP:Envelope/SOAP:Body/findResponse/result/service.SapEgrQosQueueStatsLogRecord')

但结果是空排列=> []

您可以使用 data.remove_namespaces!(参见 http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document:remove_namespaces!)。然后这个查询有效:

data.xpath('/Envelope/Body/findResponse/result/service.SapEgrQosQueueStatsLogRecord')