I have an XML I am parsing, and two elements have the same name, but require a unique value for each out of two options. I am able to get the code to function as I would like, however I am not sure why one method works (in
) and the other does not (==
).
XML snippet:
<metaMetadata>
<metadataSchema>Value1</metadataSchema>
<metadataSchema>Value2</metadataSchema>
</metaMetadata>
code:
schema_val = ['Value1', 'Value2']
for ele in root.findall(".//{*}metaMetadata"):
m_schema = ele.findall("lom:metadataSchema",ns)
m_schema_check = [i for i in schema_val if i in m_schema[0].text]
if m_schema_check:
for i in m_schema_check:
schema_val.remove(i)
if m_schema[1].text in schema_val:
print('pass')
This works as I would like it to, but I am trying to understand why it only works when I have in
, and when I have ==
the code doesn't function and skips the print
.
Edit: Would you like compare xml list values with your schema value list? If the order of the list elements doesn't matter you can sort()
the lists before comparison:
import xml.etree.ElementTree as ET
xml_str = """<metaMetadata>
<metadataSchema>Value1</metadataSchema>
<metadataSchema>Value2</metadataSchema>
</metaMetadata>"""
tree = ET.fromstring(xml_str)
l = [c.text for c in tree.findall('.//metadataSchema')]
schema_val = ['Value2', 'Value1']
if l.sort() == schema_val.sort():
print('true')
else:
print('false')
Output:
true