title: Dynamic XML template generation using get_template jinja2
body: <p>I am new in Python as well as in XML, I am using the below python code and xml file to replace the value of variables in xml file and generate another output xml file with updated value in parameter <code>{{param}}</code>.</p>
  
  <p><strong>netconf_payload.py</strong></p>
  
  <pre><code>#!/usr/bin/env python
  import os
  from jinja2 import Environment, FileSystemLoader
  PATH = os.path.dirname(os.path.abspath(__file__))
  TEMPLATE_ENVIRONMENT = Environment(
      autoescape=False,
      loader=FileSystemLoader(os.path.join(PATH, 'templates')),
      trim_blocks=False)
  def render_template(template_filename, context):
      return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
  def create_index_html():
      fname = "bridge.xml"
      param = [3, 'ieee', 3]
      context = {
          'param': param
      }
      #
      with open(fname, 'w') as f:
          html = render_template('bridge.xml', context)
          f.write(html)
  def main():
      create_index_html()
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p><strong>templates/bridge.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;vr&gt;
          &lt;vrId&gt;0&lt;/vrId&gt;
          &lt;bridge&gt;
          {% set counter = 0 -%}
          {% for param in param -%}
                  &lt;bridgeId&gt;{{ param }}&lt;/bridgeId&gt;
                  &lt;bridgeType&gt;{{ param }}&lt;/bridgeType&gt;
                  &lt;bridgeId&gt;{{ param }}&lt;/bridgeId&gt;
          {% set counter = counter + 1 -%}
          {% endfor -%}
          &lt;/bridge&gt;
  &lt;/vr&gt;
  </code></pre>
  
  <p>Now execute the command : <strong>python netconf_payload.py</strong>
  It will generate a output <strong>bridge.xml</strong> file as below :</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;vr&gt;
          &lt;vrId&gt;0&lt;/vrId&gt;
          &lt;bridge&gt;
          &lt;bridgeId&gt;3&lt;/bridgeId&gt;
                  &lt;bridgeType&gt;3&lt;/bridgeType&gt;
                  &lt;bridgeId&gt;3&lt;/bridgeId&gt;
          &lt;bridgeId&gt;ieee&lt;/bridgeId&gt;
                  &lt;bridgeType&gt;ieee&lt;/bridgeType&gt;
                  &lt;bridgeId&gt;ieee&lt;/bridgeId&gt;
          &lt;bridgeId&gt;3&lt;/bridgeId&gt;
                  &lt;bridgeType&gt;3&lt;/bridgeType&gt;
                  &lt;bridgeId&gt;3&lt;/bridgeId&gt;
          &lt;/bridge&gt;
  &lt;/vr&gt;
  </code></pre>
  
  <p>The expected output I want is: </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;vr&gt;
          &lt;vrId&gt;0&lt;/vrId&gt;
          &lt;bridge&gt;
                  &lt;bridgeId&gt;3&lt;/bridgeId&gt;
                  &lt;bridgeType&gt;ieee&lt;/bridgeType&gt;
                  &lt;bridgeId&gt;3&lt;/bridgeId&gt;
          &lt;/bridge&gt;
  &lt;/vr&gt;
  </code></pre>