<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to overwrite the contents of an element in service definition draft file ? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1134967#M63535</link>
    <description>&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;Here is how i do it:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; The xsl file looks like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;
    &amp;lt;xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" indent="yes" /&amp;gt;
    &amp;lt;xsl:strip-space elements="*"/&amp;gt;

    &amp;lt;!-- IDENTITY TRANSFORM --&amp;gt;
    &amp;lt;xsl:template match="node()|@*"&amp;gt;
     &amp;lt;xsl:copy&amp;gt;
       &amp;lt;xsl:apply-templates select="node()|@*"/&amp;gt;
     &amp;lt;/xsl:copy&amp;gt;
    &amp;lt;/xsl:template&amp;gt;

    &amp;lt;!-- REPLACE XMLDoc --&amp;gt;
    &amp;lt;xsl:template match="XmlDoc"&amp;gt;
     &amp;lt;xsl:copy&amp;gt;
       &amp;lt;xsl:apply-templates select="document('UnidataDD2MI_result.xml')"/&amp;gt;
     &amp;lt;/xsl:copy&amp;gt;
    &amp;lt;/xsl:template&amp;gt;

&amp;lt;/xsl:stylesheet&amp;gt;&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 19 Jan 2022 15:22:00 GMT</pubDate>
    <dc:creator>RehanChaudhary</dc:creator>
    <dc:date>2022-01-19T15:22:00Z</dc:date>
    <item>
      <title>How to overwrite the contents of an element in service definition draft file ?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1134967#M63535</link>
      <description>&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;Here is how i do it:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; The xsl file looks like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;
    &amp;lt;xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" indent="yes" /&amp;gt;
    &amp;lt;xsl:strip-space elements="*"/&amp;gt;

    &amp;lt;!-- IDENTITY TRANSFORM --&amp;gt;
    &amp;lt;xsl:template match="node()|@*"&amp;gt;
     &amp;lt;xsl:copy&amp;gt;
       &amp;lt;xsl:apply-templates select="node()|@*"/&amp;gt;
     &amp;lt;/xsl:copy&amp;gt;
    &amp;lt;/xsl:template&amp;gt;

    &amp;lt;!-- REPLACE XMLDoc --&amp;gt;
    &amp;lt;xsl:template match="XmlDoc"&amp;gt;
     &amp;lt;xsl:copy&amp;gt;
       &amp;lt;xsl:apply-templates select="document('UnidataDD2MI_result.xml')"/&amp;gt;
     &amp;lt;/xsl:copy&amp;gt;
    &amp;lt;/xsl:template&amp;gt;

&amp;lt;/xsl:stylesheet&amp;gt;&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 19 Jan 2022 15:22:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1134967#M63535</guid>
      <dc:creator>RehanChaudhary</dc:creator>
      <dc:date>2022-01-19T15:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to overwrite the contents of an element in service definition draft file ?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1135060#M63537</link>
      <description>&lt;P&gt;use &lt;STRONG&gt;xml.dom.minidom&lt;/STRONG&gt; instead.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is code snippet I used to modify the service definition file. You can tweak it based on your needs.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 17:30:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1135060#M63537</guid>
      <dc:creator>AzinSharaf</dc:creator>
      <dc:date>2022-01-19T17:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to overwrite the contents of an element in service definition draft file ?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1135065#M63538</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/75018"&gt;@AzinSharaf&lt;/a&gt;&amp;nbsp;&amp;nbsp;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?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 17:40:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1135065#M63538</guid>
      <dc:creator>RehanChaudhary</dc:creator>
      <dc:date>2022-01-19T17:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to overwrite the contents of an element in service definition draft file ?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1135082#M63539</link>
      <description>&lt;P&gt;do you need a modified sddraft as an output file? Can you use this?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;out_file=r"C:\Users\arcgis\out.sddraft"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 18:23:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1135082#M63539</guid>
      <dc:creator>AzinSharaf</dc:creator>
      <dc:date>2022-01-19T18:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to overwrite the contents of an element in service definition draft file ?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1136286#M63568</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/75018"&gt;@AzinSharaf&lt;/a&gt;&amp;nbsp;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:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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'&lt;/LI-CODE&gt;&lt;P&gt;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&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 08:32:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-overwrite-the-contents-of-an-element-in/m-p/1136286#M63568</guid>
      <dc:creator>RehanChaudhary</dc:creator>
      <dc:date>2022-01-24T08:32:12Z</dc:date>
    </item>
  </channel>
</rss>

