0

I want to findall tags that match

root.findall("{http://trimble.com/schema/fxl}FeatureDefinitions/{http//trimble.com/schema/fxl}PointFeatureDefinition")

or

"{http://trimble.com/schema/fxl}FeatureDefinitions/{http//trimble.com/schema/fxl}PolygonFeatureDefinition"

or

"{http://trimble.com/schema/fxl}FeatureDefinitions/{http//trimble.com/schema/fxl}PolylineFeatureDefinition"

I need to be able to grab anyone of those three tags, I think the parenthesis is throwing me off. I tried some regex and findall("*") works but after I go further I get no return.

3 Answers 3

0

You don't use a regex to parse XML, you use your XML parser.

I suggest you look at the ElementTree XPath and Namespaces documentation.

To answer your question, you can do something like the following

import xml.etree.ElementTree as et

tree = et.parse(fxl_file)
root = tree.getroot()

#Any PointFeatureDefinition elements
for pfd in root.findall('.//{http://trimble.com/schema/fxl}PointFeatureDefinition'):
    print(pfd.tag)

#Only PointFeatureDefinition elements that are the direct child of FeatureDefinitions
for pfd in root.findall('.//{http://trimble.com/schema/fxl}FeatureDefinitions/{http://trimble.com/schema/fxl}PointFeatureDefinition'):
    print(pfd.tag)

#As above but with simplified namespace handling
ns = {"fxl":"http://trimble.com/schema/fxl"}
for pfd in root.findall('.//fxl:FeatureDefinitions/fxl:PointFeatureDefinition', ns):
    print(pfd.tag)

for pfd in root.findall('.//fxl:FeatureDefinitions/fxl:PointFeatureDefinition', ns):
    print(pfd.tag)

You would repeat the findall for PolylineFeatureDefinition and PolygonFeatureDefinition elements. Unless you're using lxml, which supports a less limited XPath syntax so you could use the "or" | operator and do something like (untested):

root.findall('.//fxl:FeatureDefinitions/fxl:PointFeatureDefinition|fxl:LineFeatureDefinition|fxl:PolygonFeatureDefinition]', ns)
0

In your Regular expression, you'll have to escape some of the characters in your strings, because they are special characters in Regex syntax. Also you can group all three expressions into one easily because the only difference is the Point|Polyline|Polygon part.

The following expression should find what you want:

"\{http\:\/\/trimble\.com\/schema\/fxl\}FeatureDefinitions\/\{http\/\/trimble.com\/schema\/fxl\}(Point|Polyline|Polygon)FeatureDefinition"
0

Could not get any of the xpath or regex expressions to work. First answer was the closest to this.

   points=root.findall("{http://trimble.com/schema/fxl}FeatureDefinitions/{http://trimble.com/schema/fxl}PointFeatureDefinition")
   polygon=root.findall("{http://trimble.com/schema/fxl}FeatureDefinitions/{http://trimble.com/schema/fxl}PolygonFeatureDefinition")
   line=root.findall("{http://trimble.com/schema/fxl}FeatureDefinitions/{http://trimble.com/schema/fxl}LineFeatureDefinition")
   for child in line+polygon+points:
       ##create feature class
       fcName=child.attrib.get('Code').replace(" ","_")

Not the answer you're looking for? Browse other questions tagged or ask your own question.