title: XQuery script to create indexes
body: <p>I am trying to create a script that create indexes for a <strong>MarkLogic</strong> database. Please note the indexs shown is only a small sampling of the scripts to be created.</p>
  
  <pre><code>xquery version "1.0-ml";
  import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
  declare namespace xdmpdb = "http://marklogic.com/xdmp/database";
  
  declare variable $databaseName as xs:string := "army-itam";
  declare variable $os as xs:string := "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_OperatingSystem";
  declare variable $pe as xs:string := "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PhysicalElement";
  
  declare variable $os_xml as node() :=
  &lt;range-element-index&gt;
    &lt;index&gt;  
      &lt;type&gt;string&lt;/type&gt;
      &lt;name&gt;Name&lt;/name&gt;
    &lt;/index&gt;
    &lt;index&gt;  
      &lt;type&gt;string&lt;/type&gt;
      &lt;name&gt;Version&lt;/name&gt;
    &lt;/index&gt;  
  &lt;/range-element-index&gt;;
  
  declare variable $pe_xml as node() :=
  &lt;range-element-index&gt;
    &lt;index&gt;  
      &lt;type&gt;string&lt;/type&gt;
      &lt;name&gt;Model&lt;/name&gt;
    &lt;/index&gt;
    &lt;index&gt;  
      &lt;type&gt;string&lt;/type&gt;
      &lt;name&gt;Manufacturer&lt;/name&gt;
    &lt;/index&gt;
    &lt;index&gt;  
      &lt;type&gt;dateTime&lt;/type&gt;
      &lt;name&gt;ModelTest&lt;/name&gt;
    &lt;/index&gt;
  &lt;/range-element-index&gt;;
  
  declare function local:add-range-element-index($config as element(configuration), $dbname as xs:string, $namespace-uri as xs:string, $type as xs:string, $localname as xs:string)
  {
  try {
       let $dbid := admin:database-get-id($config, $dbname)
       let $range-index := admin:database-range-element-index($type, $namespace-uri, $localname, "http://marklogic.com/collation/", fn:false() )
       let $ExistingREindexes := fn:data(admin:database-get-range-element-indexes($config, $dbid)/xdmpdb:localname)
       let $config :=
          if ($localname = $ExistingREindexes) 
          then $config
          else admin:database-add-range-element-index($config, $dbid, $range-index)
       let $log := xdmp:log(fn:concat("ERI (", $localname, ") added"), "info")
       return $config
  } catch($e) {
       (fn:concat("Error adding ERI: ", fn:string-join($localname,",")),xdmp:log(xdmp:quote($e)))
     }
  };
  
  declare function local:create-index($config as element(configuration), $namespace-uri as xs:string, $server as node())
  {
  try {
      let $log := xdmp:log(fn:concat("Creating indexs for (", $namespace-uri, ")"), "info")
      let $config :=
        for $results in $server//index
          let $type := xs:string($results//type/text())
          let $name := xs:string($results//name/text())
          return local:add-range-element-index($config, $databaseName, $namespace-uri, $type, $name) 
      return $config
  } catch($e) {
       xdmp:log(xdmp:quote($e))
     }
  };
  
  declare function local:index-create($config as element(configuration))
  {
  try {
       let $config := local:create-index($config, $os, $os_xml)
       let $config := local:create-index($config, $pe, $pe_xml)
       return $config
  } catch($e) {
       xdmp:log(xdmp:quote($e))
     }
  };
  
  let $config := admin:get-configuration()
  let $config := local:index-create($config)
  return admin:save-configuration($config)
  </code></pre>
  
  <p>When I run this script, it only creates one index from each xml list defined, either the first or the last one. When I check the logs, the <code>local:add-range-element-index</code> function states that it processed all elements. </p>
  
  <p>Also, the log says that the second xml list repeats by the number of index elements, <em>example of log</em>.</p>
  
  <pre><code>2013-10-03 15:20:52.291 Info: App-Services: Creating indexs for(http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_OperatingSystem)
  2013-10-03 15:20:52.634 Info: App-Services: ERI (Name) added
  2013-10-03 15:20:52.712 Info: App-Services: ERI (Version) added
  2013-10-03 15:20:52.712 Info: App-Services: Creating indexs for (http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PhysicalElement)
  2013-10-03 15:20:52.751 Info: App-Services: ERI (Model) added
  2013-10-03 15:20:52.789 Info: App-Services: ERI (Manufacturer) added
  2013-10-03 15:20:52.856 Info: App-Services: ERI (ModelTest) added
  2013-10-03 15:20:52.856 Info: App-Services: Creating indexs for (http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PhysicalElement)
  2013-10-03 15:20:52.895 Info: App-Services: ERI (Model) added
  2013-10-03 15:20:52.932 Info: App-Services: ERI (Manufacturer) added
  2013-10-03 15:20:52.970 Info: App-Services: ERI (ModelTest) added
  </code></pre>