1

I am using server specific data and want to parse data "measTypes" in my xml file. because of some header data in my xml file (namespaces), I could not parse data, and my code fails, Could you help me to fetch "measTypes" in my xml data?

I am using below code, but it fails because measInfo has no value :

from lxml import etree
tree = etree.parse(open("BLRNCH03.xml"))
measInfo = tree.xpath('//measInfo[@measInfoId="67109488"]')[0]
print(measInfo)

here is my xml data :

<?xml version="1.0" encoding="UTF-8"?>
<measCollecFile xmlns="http://latest/nmc-omc/cmNrm.doc#measCollec" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://latest/nmc-omc/cmNrm.doc#measCollec schema\pmResultSchedule.xsd">
    <fileHeader fileFormatVersion="32.435 V7.2" vendorName="Huawei">
        <fileSender elementType="BSC6910 UMTS"/>
        <measCollec beginTime="2018-04-22T00:00:00+04:30"/>
    </fileHeader>
    <measData>
    <measInfo measInfoId="67109481">
        <measTypes>67194793 67194794 67194795 67194796 </measTypes>
    </measInfo>
    <measData>
    <fileFooter>
        <measCollec endTime="2018-04-22T01:00:00+04:30"/>
    </fileFooter>
</measCollecFile>

1 Answer 1

3

Just bind the default namespace to a prefix and use it in the .xpath() call.

I used the prefix mc, but you could use something different.

Example...

from lxml import etree

namespaces = {"mc": "http://latest/nmc-omc/cmNrm.doc#measCollec"}

tree = etree.parse("BLRNCH03.xml")
measTypes = tree.xpath("//mc:measInfo[@measInfoId='67109481']/mc:measTypes", 
                      namespaces=namespaces)[0]

print(measTypes)

This will print something like:

<Element {http://latest/nmc-omc/cmNrm.doc#measCollec}measTypes at 283e638>
Sign up to request clarification or add additional context in comments.

3 Comments

I'll give you an up vote so at least you get some of the points you deserve :)
@DanielHaley, sorry, I am new here, I did. My ponts are less than 15, I cannot give point to you, sorry about that :)
@Sadegh - That's quite ok. Thank you for accepting my answer. Welcome to Stack Overflow!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.