Gazebo Diary – Vol. 5 (SDF Validator-2)

Binnur Görer · February 21, 2014

Upon obtaining the XSD files generated for the SDF (Simulator Description Format) files of Gazebo, I have validated the text in the editor again the XSD files. If the text does not meet the format described by the XSD files, then the editor highlights the line that causes the error in red and shows an error message in the box located just below the editor.

gazebo1

 

#! /usr/bin/python
import urllib2

def download_file(file_url, file_name):
  print "download " + file_name + " from " + file_url
  response = urllib2.urlopen(file_url)
  content = response.read()
  schema_file = open(file_name, 'w')
  schema_file.write(content)
  schema_file.close()

file_list = open('file_list.txt')
for line in file_list:
  file_name = line.strip('\\n')
  file_url = "http://sdformat.org/schemas/" + file_name
  download_file(file_url, file_name)

By this script, we can download all XSD files from the link. The file_list.txt includes the names of all XSD files.

#!/usr/bin/python
from xml.dom.minidom import parse

def get_file_ref(file_url):
  index = file_url.rfind('/')
  file_name = file_url[index+1:]
  return './' + file_name

def replace_includes(xml_file):
  dom = parse(xml_file)
  include_nodes = dom.getElementsByTagName('xsd:include')
  for node in include_nodes:
    node.attributes['schemaLocation'].nodeValue = get_file_ref(node.attributes['schemaLocation'].nodeValue) 
    print node.attributes['schemaLocation'].nodeValue

file_handle = open(xml_file, 'w')
dom.writexml(file_handle)
file_handle.close()

file_list = open('file_list.txt')
for line in file_list:
  file_name = line.strip('\\n')
  replace_includes(file_name)

By this script, we can replace includes with local files. For example, the line <xsd:include schemaLocation=”http://sdformat.org/schemas/types.xsd”/> is replaced by the location of the types.xsd file which is downloaded before by utilizing the above script.

Twitter, Facebook