I have the following XML element:
<Content>
<Controller Use="Context" Name="Base_Project_Maximum_Connections">
<DataTypes Use="Context">
<DataType Use="Target" Name="cs_dint" Family="NoFamily" Class="User">
<Members>
<Member Name="Status" DataType="CONNECTION_STATUS" Dimension="0" Radix="NullType" Hidden="false" ExternalAccess="Read/Write" />
<Member Name="Data" DataType="DINT" Dimension="0" Radix="Decimal" Hidden="false" ExternalAccess="Read/Write" />
</Members>
</DataType>
</DataTypes>
</Controller>
</Content>
I am trying to find all DataType elements that have a Name of "cs_dint" using the following code:
def GetDataTypeFromElement(root, typeName = None):
dataTypeIter = root.iter("DataType")
for dataTypeElement in dataTypeIter:
print ("Found a data type element")
print ("Finished with first iteration")
dataTypeIter = root.iter("DataType[@Name = 'cs_dint']")
for dataTypeElement in dataTypeIter:
print ("Found a data type element")
print ("Finished with second iteration")
return None
The first call to iter(), where I merely look for DataType elements, works. The second, where I try to specify the name, doesn't. Why not?
The parameter passed to the iter
method is not an XPath expression but only an element name. To query using XPath, you need to use one of the methods with find
in its name, such as iterfind
or findall
.