3

I'm new both to this forum and Python, and I'm having trouble tracking down a solution to my problem. I'm hoping someone can offer some process-improvement advice and/or code to help get my metadata issue sorted. This is the first script I've attempted so I imagine there's a better way to do what I'm looking for.

My Goal:

Create a script to:

• import metadata and have it reflect the correct information about the new file • remove linkage, lineage, and copyHistory elements or text (and any other reference to location on our servers) for public sharing of file

Current process:

• Python script to apply metadata based on an existing XML file using arcpy.ImportMetadata_conversion, sync enabled. • Remove elements from XML file using minidom removeChild.

# Step 4: Import metadata

# polygon file
arcpy.ImportMetadata_conversion ("metadatafile.xml", "FROM_ARCGIS", polyfilefull, "ENABLED")



#Step 5: Remove identifying information from metadata
# based on http://stackoverflow.com/questions/5699745/how-to-comment-out-an-xml-element-using-minidom-dom-implementation
# added "utf-8" to avoid a whitespace error, appears to work

from xml.dom import minidom

polydoc = minidom.parse(polyfilefull + ".xml")
    for linkage in polydoc.getElementsByTagName('linkage'):
    linkage.parentNode.removeChild(linkage)
for lineage in polydoc.getElementsByTagName('lineage'):
    lineage.parentNode.removeChild(lineage)
for copyHistory in polydoc.getElementsByTagName('copyHistory'):
    copyHistory.parentNode.removeChild(copyHistory)
f = open(polyfilefull + ".xml", "w")
f.write(polydoc.toxml("utf-8"))
f.close()

Issues:

• “Linkage” reappears when metadata is viewed in Arc. • Want to update publication date based on current date

Possible solutions:

• Remove the text of the element rather than the element itself, and remove ‘sync’ from the element or • change the value of sync to FALSE.

1

I've found a solution. I can set the text as empty ('') and then change the attribute value for Sync to FALSE. This keeps the fields from popping up again the next time I view the metadata in ArcGIS. I also added the update for the publication date.

This really was just a matter of looking through forums until I found the right answer, but for some reason I had a very hard time finding something to explain the simple practice of editing attribute values. As soon as I asked I came across the answer, as it goes.

from xml.dom import minidom

polydoc = minidom.parse(polyfilefull + ".xml")

for linkage in polydoc.getElementsByTagName('linkage'):
    linkage.firstChild.nodeValue=''
    linkage.setAttribute("Sync", "FALSE")
for pubDate in polydoc.getElementsByTagName('pubDate'):
    pubDate.firstChild.nodeValue=time.strftime("%Y%m%d")

for lineage in polydoc.getElementsByTagName('lineage'):
    lineage.parentNode.removeChild(lineage)
for copyHistory in polydoc.getElementsByTagName('copyHistory'):
    copyHistory.parentNode.removeChild(copyHistory)

f = open(polyfilefull + ".xml", "w")
f.write(polydoc.toxml("utf-8"))
f.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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