arcpy.XSLTransform_conversion Functionality in Pro --> Location of xml Templates

05-05-2021 09:55 AM
Labels (2)
New Contributor III

Hi everybody.  Trying to "stamp" (term I made up - makes sense?) metadata in feature classes within file geodatabase utilizing arcpy functionality in Pro - looping through an entire gdb, so many fcs. 

I am cannibalizing pieces from this script: .  esri forum metadata post.  Note that I am NOT interested in the Thumbnail portion, just basic workflow to update Item Description via: 1) pull xml from feature classes within gdb 2) update a handful of xml elements (abstract, purpose, credits, etc.) in xml 3) update xml in gdb.  I am utilizing xmltree and pandas dataframes in Python scripts for the workflow FYI. 

My dumbed-down pseudocode is as follows: 

fcs = 'path/to/feature_class'
# below line yields - 'C:\Program Files\ArcGIS\Pro'
dir = arcpy.GetInstallInfo('desktop')['InstallDir']
# but exact copy of.xslt is NOT here - can be found in desktop folder
copy_xslt = r'{0}'.format(os.path.join(dir,'Metadata\Stylesheets\gpTools\exact copy of.xslt'))
# temporary XML file
xml_file = arcpy.CreateScratchName('.xml',workspace=arcpy.env.scratchFolder)
xml_file = 'name.xml'
XSLTransform_conversion(fcs, copy_xslt, xml_file,'')
arcpy.MetadataImporter_conversion(xml_file, fcs)

The main thing confusing me is that

dir = arcpy.GetInstallInfo('desktop')['InstallDir']

YIELDS: C:\Program Files\ArcGIS\Pro\Resources\Metadata\Stylesheets  but does NOT have the "exact copy of.xslt".  If I go 'C:\Program Files (x86)\ArcGIS\Desktop10.6\Metadata\Stylesheets' then it exists.  However the online documentation indicates that the Pro file ...Stylesheets subdirectory should have that file.  Any idea why I don't have it and should I be concerned?  I will basically just hardcode the file path the xslt file while running scripts via Pro.

0 Kudos
1 Reply
New Contributor III

If a tree in the falls in the forest and no one's around to hear it in practice:

Well, I figured it out.  Should have read the ArcPro docs more thoroughly because they have basically simplified (in my view) the syntax and process to copy metadata in the updating xml from gdb process (arcpro docs ).  Instead of referencing an xslt file ('exact copy of.xslt') in the bowels of the ArcPro folder structure as an argument in the Desktop XSLTransformation_conversion function , they replaced it with an argument in the Pro version of the function called arcpy.saveAsXML in which you pass a string argument - in this case 'EXACT COPY' which performs the same purpose of  `exact copy of.xslt'.  'EXACT COPY' most likely transforms your metadata using that same xslt without having to explicitly pass the file path.


from arcpy import metadata as md
fp_fcs = 'path/to/fcs'
tgt_item_md = md.Metadata(fp_fcs)
xml_file = arcpy.CreateScratchName('.xml', workspace = arcpy.env.scratchFolder)
tgt_item_md.saveAsXML(xml_file, 'EXACT_COPY')
# From here - update the xml_file and then update the target: Update xml file using xml.etree.ElementTree protocol - examples all over the internet - in this case to update my Item Description 'idPurp' or 'idAbstract'
src_template_md = md.Metadata(xml_file)


  Someday in four years somebody will stumble across this and hopefully not get dumber...

0 Kudos