ArcGIS 10 SP3
I have read the other posts in this forum about updating metadata programmatically, and have examined Esri's help topics regarding xslt stylesheets to update metadata, but am hoping that someone has produced a more efficient solution in the last ten months:
I would like to perform something like the following examples:
- for all layer files which contain the word "road" in their name, add the following list of words to their tags: road, street
- for all SDE feature classes which do not currently have an Item Description, add the feature class name and tags into the Item Description
etc.
My question is: can I do this with python or VBA, or must I use xml/xslt stylesheets to do this?
Best Answer
ElementTree
does allow one to programatically edit XML metadata files. I've used this on a recent project to update many tags using information stored in a database. So basically for each shapefile I have a data description, citation, abstract, etc. stored in a table and access the tags usingElementTree
and retrieving the metadata using a search cursor. I'm not an expert on the structure of theElementTree
library, but the long and short of it is that you create an "iterator" object which is the parent tag of the "subelement(s)" you want to edit. Say, for example you have the following tags and you want to change the publisher information:You could write something like the following snippet in python:
Note that the iterator object will search the entire XML file looking for the tag "pubinfo". If multiple tags share the same name, the iterator object will contain one element for each occurrence. In this case you will have to dig through your XML files to make sure you are working on the correct one. Say there are 2 instances of the tag
pubinfo
(one could be for the "local citation" and one could be for the "larger work citation") and you only want to change the subelementpublish
in first one you would replace thefor elem in iterator
withelem = iterator[0]
. If you need more I can provide you with some scripts I wrote, though I'll refrain from posting them in their entirety here.