Re: [Generateds-discuss] How to create/ generate new xml from generated API
Brought to you by:
dkuhlman
From: Dave K. <dku...@da...> - 2022-06-01 23:08:26
|
Daniel, Thanks for taking the time to report this. Here is what I've tried so far: 1. I copied TestResults.xml and Common.xsd to a directory. 2. I ran the following command: ./generateDS.py -f -o tmp01sup.py -s tmp01sub.py --super tmp01sup --member-specs=dict '--export=write etree validate' TestResults.xsd That generated a module `tmp01sup.py`. 3. So, I started IPython (the plain Python interactive prompt would also work) and did this: [ins] In [1]: import tmp01sup as lib Which also worked. 4. Then I created an instance of a class in the generated module: [ins] In [5]: testresults01 = lib.TestResult() [ins] In [6]: testresults01.export(sys.stdout, 0) <c:TestResult xmlns:None="http://www.ieee.org/ATML/2006/Common" xmlns:c="http://www.ieee.org/ATML/2006/Common" /> Which also seemed to work. So, now I've got to read your message a bit more carefully so that I can figure out what is wrong for your case and how to fix it. I do not have the file `collect_schema_locations.py`, which you show in your message as generating an exception. So, I can't test that. Did you generate that file with `generateDS.py`? However the exception "TypeError: empty namespace prefix is not supported in XPath" seems to occur when you have a key in the nsmap that is `None`. Try removing it. Then do the `xpath` search: [ins] In [23]: nsmap = root.nsmap [ins] In [24]: nsmap Out[24]: {'xs': 'http://www.w3.org/2001/XMLSchema', 'c': 'http://www.ieee.org/ATML/2006/Common', None: 'http://www.ieee.org/ATML/2007/TestResults'} [ins] In [25]: nsmap.pop(None) Out[25]: 'http://www.ieee.org/ATML/2007/TestResults' [ins] In [26]: nsmap Out[26]: {'xs': 'http://www.w3.org/2001/XMLSchema', 'c': 'http://www.ieee.org/ATML/2006/Common'} [ins] In [27]: root.xpath('xs:import', namespaces=nsmap) Out[27]: [<Element {http://www.w3.org/2001/XMLSchema}import at 0x7faf573acac0>] Does that help with your work? By the way, the "--one-file-per-xsd" capability is something that I do not understand, myself. That code was contributed by someone else. Dave Quoting "Dewitte, Daniel W (US) via Generateds-discuss" <gen...@li...>: > Here are the actual files this time :) > > Daniel DeWitte > > T: +1-603-885-2602 > M: +1-603-759-4476 > E: dan...@ba...<mailto:dan...@ba...> > > From: Dewitte, Daniel W (US) > Sent: Wednesday, June 1, 2022 1:06 PM > To: 'Gen...@li...' > <Gen...@li...> > Subject: How to create/ generate new xml from generated API > > Hi Dave, > > This is one of the better tools that I have found and I think the > only one that can work the way I need it too, I just need some help > with getting to the finish line. > > My goal is generate a new .xml using a known schema .xsd with Python > as the base language (required). I have 13 different reports that > all have a different format that are generated on a constant basis > that I need to import into a known .xsd format for upload to a > database. > > I believe I either want to use the build methods, or the "one-per" > options, but I can't get either of them to work. I asked about the > build method problem on stackover flow here: Stackoverflow - > Creating a new XML from an XSD using GenerateDS and > Python<https://stackoverflow.com/questions/72397436/creating-a-new-xml-from-an-xsd-using-generateds-and-python> > > But I am not sure that the build method is what I want to use. > > When I try the "one-per" I get the following errors: > Command line method, I believe this error is due to that the .xsd is > in a UTF-8 encoding. How do I get this to work with UTF-8 encoding? >> (base) H:\Python Training\GenerateDS_Docs>python generateds.py >> --one-file-per-xsd --output-directory="OnePer" >> --module-suffix="One" TestResults.xsd >> Traceback (most recent call last): >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9906, >> in <module> > main() >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9872, in main > parseAndGenerate( >> File "H:\Python Training\GenerateDS_Docs\generateds.py", line 9318, >> in parseAndGenerate > parser.parse(rootFile) >> File "C:\EngTools\Anaconda3\lib\xml\sax\expatreader.py", line 111, in parse > xmlreader.IncrementalParser.parse(self, source) >> File "C:\EngTools\Anaconda3\lib\xml\sax\xmlreader.py", line 123, in parse > buffer = file.read(self._bufsize) >> File "C:\EngTools\Anaconda3\lib\encodings\cp1252.py", line 23, in decode > return codecs.charmap_decode(input,self.errors,decoding_table)[0] >> UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in >> position 4013: character maps to <undefined> > > Approach 2 - Extraction and Generation Utilities: >> (base) h:\Python Training\GenerateDS_Docs\utils>python >> collect_schema_locations.py --force --verbose TestResults.xsd >> testJson.txt >> Traceback (most recent call last): >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 157, in <module> > main() >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 148, in main > extract_and_generate(infile, outfile, extract_locations_fn, options) >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 86, in extract_and_generate > locations = extract_locations(infile, options) >> File "h:\Python >> Training\GenerateDS_Docs\utils\collect_schema_locations.py", line >> 50, in extract_locations > elements = root.xpath('xs:import', namespaces=root.nsmap) >> File "src/lxml/etree.pyx", line 1579, in lxml.etree._Element.xpath >> File "src/lxml/xpath.pxi", line 259, in >> lxml.etree.XPathElementEvaluator.__init__ >> File "src/lxml/xpath.pxi", line 131, in >> lxml.etree._XPathEvaluatorBase.__init__ >> File "src/lxml/xpath.pxi", line 55, in lxml.etree._XPathContext.__init__ >> File "src/lxml/extensions.pxi", line 81, in lxml.etree._BaseContext.__init__ >> TypeError: empty namespace prefix is not supported in XPath > > Attached are the .xsd that I am using. TestResults.xsd references > Common.xsd. > > Any help on getting started with my overall goal will be much > appreciated. I am looking to create a new XML from a known .xsd and > add in the elements and child elements and attributes as needed. > > Thank you for the help, > Daniel DeWitte -- Dave Kuhlman http://www.davekuhlman.org |