Getting value from Metadata in Python Script for attribute?

is creating a standalone .xml file for the feature class an option? ... for instance a temporary XML, which you delete extracting the information you need?

If yes, you might want to look into the arcpy command "XSLTransform_conversion". To get acquainted with it, look into ArcToolbox: Conversion Tools > Metadata toolset. The arcpy command only calls this tool.

Essentially, this tool allows you to convert metadata from a format to another. In your case, you could expose the feature class metadata into a XML file using the tool's parameter "XSL sheet" called "exact copy of". These XSL sheets are available in your ArcGIS install folder, under ..\Metadata\Stylesheets\gpTools. As it says, this particular XSL sheet will make NO modification to the contents on the feature class metadata, it will only export it into a XML file.

ESRI help on this tool is available here, and here is further help on the metadata toolset.


Here is the code that finally did the trick. This is the same method proposed by Helene above. Any comments on how to improve this are welcome.

    #Create XML from RCI Basemap Metadata
    translator = "C:\Program Files (x86)\ArcGIS\Desktop10.2\Metadata\Translator\ARCGIS2FGDC.xml"
    arcpy.ExportMetadata_conversion(roads,translator,arcpy.env.workspace+"\\"+"roads_Export.xml")

    #Get MAPVERSION from RCI Basemap
    from xml.etree.ElementTree import ElementTree
    from xml.etree.ElementTree import Element, SubElement

    xmlfile = arcpy.env.workspace+"\\"+"roads_Export.xml"

    tree = ElementTree()
    tree.parse(xmlfile)

    spot = tree.find("idinfo/citation/citeinfo/pubdate")
    pub_date = spot.text

    year = pub_date[0:4]
    month = pub_date[4:6]
    month_num = int(month)

    if 0 < month_num < 3:
        version = year+"Q1"
    elif 3 < month_num < 6:
        version = year+"Q2"
    elif 6 < month_num < 9:
        version = year+"Q3"
    elif 9 < month_num < 12:
        version = year+"Q4"

    #Calculate Values for Coordsys, Mapsource, MapVersion, publish date, CAR xfer date, coord xfer date

    arcpy.CalculateField_management(input,"MAPVERSION",'"{0}"'.format(version),"PYTHON")

Tags:

Metadata

Arcpy