2

I've been trying to get the value of the ci-name element whose sibling RandomAttribute's value(text) is IMPORTANT. I'm a beginner to python and I'm trying using Python's inbuilt ElementTree.

Here's the sample XML:

<state>
    <s-name>State 1</s-name>
    <district>
        <d-name>District 1</d-name>
        <city>
            <ci-name>City 1</ci-name>
            <RandomAttribute>UN- IMPORTANT</RandomAttribute>
        </city>
        <city>
            <ci-name>City 2</ci-name>
            <RandomAttribute>NOT SO IMPORTANT</RandomAttribute>
        </city>
        <city>
            <ci-name>City 3</ci-name>
            <RandomAttribute>IMPORTANT</RandomAttribute>
        </city>
    </district>
</state>

Please help me out with this.

2
  • Can you use lxml? Commented Nov 28, 2019 at 18:35
  • @Jack Fleeting Yes Commented Nov 28, 2019 at 18:37

3 Answers 3

4

You can access the value with the XPath-1.0 expression

/state/district/city[RandomAttribute='IMPORTANT']/ci-name

Just put this into a Python XML processor.
In lxml this can look like

xml = ...
tree = etree.parse(xml)
result = tree.xpath('/state/district/city[RandomAttribute='IMPORTANT']/ci-name')
print(result[0].tag)

Output should be

City 3

Sign up to request clarification or add additional context in comments.

Comments

2

Not a solution in one line like the previous one, but I like to keep track of how the XML is structured.

Maybe this helps you understand how ElementTree works?

import xml.etree.ElementTree as ET

xml = 'temp.xml'
xmltree = ET.parse(xml)
cities = xmltree.findall('district')[0].findall('city')
for city in cities:
    RandAttribute = city.findall('RandomAttribute')[0].text
    if RandAttribute == "IMPORTANT":
        ci_name = city.findall('ci-name')[0].text
        print(ci_name)

Comments

0
import lxml.etree as etree

if xml you are taking about is string then

xml_xpath = "/state/district/city[RandomAttribute='IMPORTANT']/ci-name/text()"
XML_dom = etree.XML(xml)
XML_XPATH_name = etree.XPath(xml_xpath)
return XML_XPATH_name(XML_dom)

Comments

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.