Carlos,

Thanks for these ideas.

Your first suggestion (adding all name space definitions to the root
node seems good to me.  Here is a set-comprehension that can be
applied to an Lxml element tree and will create that collection of
definitions:

    namespacedefs = ' '.join({
        'xmlns:{}="{}"'.format(prefix, uri)
        for node in rootNode.iter()
        for (prefix, uri) in node.nsmap.items()
        if prefix is not None})

One comment about the above solution -- There could be an XML
document where the definition of a name space prefix could be
overridden and changed at a lower, nested level in the document.
That case seems pathological to me, maybe even maligant and evil.
But, then I suppose we programmers are supposed to be paranoid
enough to worry about those kinds of cases.

I'll send a patched version of the code that I sent yesterday.  This
one will use the above collection.  It also has a function that is
effectively the equivalent of the contorted set comprehension above,
but is easier to read.

Your second suggestion ("Just an idea") is more involved.  It will
take me awhile to think it through and to figure out how to
implement it.  I don't understand it yet.  But, it smells like it
might be an even better and more comprehensive solution.

I suspect that, if we want a robust solution to this problem, then,
as you seem to suggest, each generated class must be able to
preserve the name space prefix and URI its element had in the input
XML document.  We might, also, have to pay attention to attributes
names, which can also have namespace prefixes.

Thanks for motivating me to think about this.  My half-way-there
patches have been bothering me.

More later.

Dave

On Tue, Jun 11, 2019 at 10:31:35AM +0100, Carlos AG wrote:
> Thank you very much Dave for looking into this.
> 
> I understand not wanting to clutter the xml with all the name space
> definitions.
> 
> Could there be a way to add all of them only to the root element?
> 
> Just an idea:
> 
>   * Every class could have a "self.default_namespace" and a the
>     correspondent "get" method
>   * Every class has a get "getChildrenNamespace()" method that returns a set
>   * In the export method in the same way that the "exportChildren()" is
>     called the "getChildrenNamespace()" could be called. Each element
>     will collect it's children's namespaces and if its own one is not
>     there add it to the set and pass it back to the parent.
> 
> Thanks again for your time.
> 
> All the best,
> 
>    Carlos
> 
> 
> 
> On 11/06/2019 00:50, Dave Kuhlman wrote:
> > Carlos,
> >
> > Here are the two files I said that I'd attach.
> >
> > I'll be back with more later, I hope, when I've had time to work on
> > this some more.
> >
> > Dave
> >
> -- 
> 
> Carlos Alonso-Gabizon - Carbon Co-op - Technical Developer
> 
> m: 07990 725 966
> 
> e: car...@carbon.coop
> w: https://carbon.coop
> t: @carboncoop
> 
> People Powered Not Fossil Fuelled: an intro to Carbon Co-op:
> http://www.carbon.coop/reports/CarbonCoopPeoplePoweredBooklet.pdf
> 
> Office: c/o URBED, 10 Little Lever Street, Manchester M1 1HR
> 
> Society for the Reduction of Carbon Ltd (Trading as Carbon Co-op)
> Registered address: Holyoake House, Hanover Street, Manchester M60 0AS United 
> Kingdom
> Registered under the Industrial and Provident Societies Act 1965 
> Registration Number 31342R
> 

-- 

Dave Kuhlman
http://www.davekuhlman.org


_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to