How to overwrite the contents of an element in service definition draft file ?

701
4
01-19-2022 07:22 AM
RehanChaudhary
Occasional Contributor

I am trying to overwrite the default metadata stored in the service definition draft file under the "XmlDoc" element tag. The problem that i am facing is that when i overwrite it ,my file changes into the standard xml file probably because of the library and workflow that i am using for changing it. How can i do it without changing the actual structure of draft file?

Here is how i do it:

import xml.etree.ElementTree as ET
import os
import sys
import json
import arcgisscripting
import arcpy
import arcgis
from arcgis.gis import GIS
import xml.dom.minidom as DOM
import shutil
import xml.etree.ElementTree as ET
import netCDF4
import lxml.etree as et 
rasterimage = r"C:\Users\arcgis\schism.crf"
output_draft = r"C:\Users\arcgis\airquality.sddraft"
service = "airquality"
con = r"C:\Users\arcgis\rasterimage.ags"
arcpy.CreateImageSDDraft(rasterimage, output_draft, service,
                         'FROM_CONNECTION_FILE', con, True, None, "netCDF test", "schism,netcdf")


metadata_path=r"C:\Users\arcgis\UnidataDD2MI_result.xml"
xsl_file=r"C:\Users\arcgis\MyScript.xsl"
out_file=r"C:\Users\arcgis\out.xml"

doc = et.parse(output_draft) 
xsl = et.parse(xsl_file) 
transform = et.XSLT(xsl) 
result = transform(doc) 
print(result)
result.write_output(out_file)
sys.exit(0)

  The xsl file looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <!-- IDENTITY TRANSFORM -->
    <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
    </xsl:template>

    <!-- REPLACE XMLDoc -->
    <xsl:template match="XmlDoc">
     <xsl:copy>
       <xsl:apply-templates select="document('UnidataDD2MI_result.xml')"/>
     </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
0 Kudos
4 Replies
AzinSharaf
Occasional Contributor II

use xml.dom.minidom instead. 

This is code snippet I used to modify the service definition file. You can tweak it based on your needs.

import xml.dom.minidom as dom

doc = dom.parse(sddraft_output_filename)

# Find all elements named TypeName. This is where the server object extension
# (SOE) names are defined.
typeNames = doc.getElementsByTagName('TypeName')
for typeName in typeNames:
    # Get the TypeName we want to enable.
    if typeName.firstChild.data == "FeatureServer":
        extension = typeName.parentNode
        for extElement in extension.childNodes:
            # Enable Feature Access.
            if extElement.tagName == 'Enabled':
                extElement.firstChild.data = 'true'

# Output to a new sddraft.
sddraft_mod_xml = service + '_mod_xml' + '.sddraft'
sddraft_mod_xml_file = os.path.join(sddraft_folder, sddraft_mod_xml)
f = open(sddraft_mod_xml_file, 'w')
doc.writexml(f)
f.close()

 

 

0 Kudos
RehanChaudhary
Occasional Contributor

@AzinSharaf  i don't think minidom can be used for replacing elemtns in service definition file. as you have worked with it, i will explain it in detail whjat i want to do. i have an xml file which contains metadat and i want this to be integrated into the service definition file in the "XmlDoc" tag. if you see the service definition file, you will see it contains a "Xml Doc" tag right at the end. is there a possibility of doing it with minidom?

0 Kudos
AzinSharaf
Occasional Contributor II

do you need a modified sddraft as an output file? Can you use this?

out_file=r"C:\Users\arcgis\out.sddraft"

 

0 Kudos
RehanChaudhary
Occasional Contributor

@AzinSharaf yes i need a modified sddraft as an output file but the above statement that you have mentioned is not working. When i use the above statement it changes the format of sddraft to something different and then i cannot enable WMS and feature service in the way that you have mentioned in your code and how i am also doing in following code:

xsl_file=r"C:\Users\Chaudhr1\arcgis\MyScript.xsl"
out_file=r"C:\Users\Chaudhr1\arcgis\out.sddraft"

doc = et.parse(output_draft) 
xsl = et.parse(xsl_file) 
transform = et.XSLT(xsl) 
result = transform(doc) 
print(result)
result.write_output(out_file)
soe = 'WMSServer'
doc = DOM.parse(out_file)
typeNames = doc.getElementsByTagName('TypeName')
descriptions = doc.getElementsByTagName('Type')
newType = 'esriServiceDefinitionType_Replacement'
keys=doc.getElementsByTagName('Key')
for key in keys:
     if key.hasChildNodes():
         if key.firstChild.data == 'PackageIsPublic':
             key.nextSibling.firstChild.data = 'true'

is there a way that i create sddraft file only once and then modify it keeping its original format intact? i would appreciate any help

0 Kudos