Skip to content

Instantly share code, notes, and snippets.

@joeynebula
Created November 8, 2016 21:01
Show Gist options
  • Select an option

  • Save joeynebula/f5067c7c3ff9e470b53c0522ed591767 to your computer and use it in GitHub Desktop.

Select an option

Save joeynebula/f5067c7c3ff9e470b53c0522ed591767 to your computer and use it in GitHub Desktop.
Create github markdown file showing all required fields from an xsd file
#!/usr/bin/python
import sys
import getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
process(inputfile, outputfile)
# print 'Output file is "', outputfile
def process(inputFile, outFile):
# read the file line by line into an array
fname = inputFile
with open(fname) as f:
content = f.readlines()
# open a file to write to
with open(outFile, "w") as of:
# check each line for an element
types = []
for line in content:
# check for an element
if "xs:element" in line:
name, Required = getNameRequired(line)
if "Resp" in name or "Req" in name:
formatingChar = "##"
if len(name) > 0:
of.write("\n{0} {1} \n - {2} Required\n".format(formatingChar, name, Required))
else:
complexType = checkIfComplexType(name, types)
formatingChar = "-"
if len(name) > 0:
if complexType is not None:
of.write("\n{0} [{1}]({2}) \n - {3} Required"
.format(formatingChar, name,
complexType['formartStr'], Required))
else:
of.write("\n{0} {1} \n - {2} Required\n"
.format(formatingChar, name, Required))
elif "complexType" in line:
splitLines = line.split()
if len(splitLines) > 1:
name = cleanString(splitLines[1])
formartStr = "\n# {0}\n\n".format(name)
types.append({'name': name,
"formartStr":
"#{0}".format(name.lower())})
of.write(formartStr)
print(types)
def cleanString(_str):
return _str.split("=")[1].replace('\"', "").replace(">", "")
def checkIfComplexType(name, elements):
return next((item for item in elements if item["name"] == "{0}Type".format(name)), None)
def getNameRequired(line):
# check for a minOccurs
required = not ("minOccurs=\"0\"" in line or "nillable=\"true\"" in line)
# if we don't have a minOccurs flag the entry as required
# if it has an element split the line on spaces
splitLines = line.split()
if len(splitLines) > 1:
# grab the second elment and place it in the name entry
name = cleanString(splitLines[1])
requiredStr = "[ ]"
if required:
requiredStr = "[x]"
# <xs:element name="AgencyID"
# type="dtatyp:AgencyIDType" minOccurs="0"/>
# add the entry to the dictionary list
return name, requiredStr
else:
return "", ""
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment