Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TriX serializer places two xmlns attributes on same element #250

Closed
uholzer opened this issue Jan 25, 2013 · 2 comments
Closed

TriX serializer places two xmlns attributes on same element #250

uholzer opened this issue Jan 25, 2013 · 2 comments
Labels
bug Something isn't working fix-in-progress

Comments

@uholzer
Copy link
Contributor

uholzer commented Jan 25, 2013

When I have a ConjunctiveGraph with the default namespace set, for example

>>> import rdflib
>>> g = rdflib.ConjunctiveGraph()
>>> g.bind(None, "http://defaultnamespace")

then the Trix serializer binds the default namespace twice in its XML output, once for the Trix namespace and once for the namespace I used:

>>> print(g.serialize(format='trix').decode('UTF-8'))
<?xml version="1.0" encoding="utf-8"?>
<TriX
  xmlns:xml="http://www.w3.org/XML/1998/namespace"
  xmlns="http://defaultnamespace"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns="http://www.w3.org/2004/03/trix/trix-1/"
/>

I don't know how one should fix this. The problem is with the function TriXSerializer.serialize or the function XMLWriter.namespaces. TriXSerializer.serialize could remove the namespace with the empty prefix or XMLWriter.namespaces could give precedence to the namespaces in extra_ns over those in namespace.

@ghost
Copy link

ghost commented Jan 26, 2013

++ TriXSerializer.serialize could remove the namespace with the empty prefix
++ or XMLWriter.namespaces could give precedence to the namespaces in
++ extra_ns over those in namespace

I tip my hat to you for pointing out the alternative. The latter is a clear winner under the principle of "least surprise". It's a bit unfortunate that the regrettably vague "extra_ns" doesn't signal the precedence, rather the opposite. However, a very straightforward guard and a comment at least renders the semantics explicit in the codebase and AFAICT, XMLWriter isn't part of the published API

Fixed in commit #785071c76b

Cheers,

@ghost ghost closed this as completed Jan 26, 2013
@uholzer
Copy link
Contributor Author

uholzer commented Jan 27, 2013

Thanks for the flowers.

I am still suspecting an issue though: After inspecting your commit I don't think it works as I imagined it. You wrote

        for prefix, namespace in namespaces:
            if prefix:
                write('  xmlns:%s="%s"\n' % (prefix, namespace))
            # Allow user-provided namespace bindings to prevail
            elif prefix not in self.extra_ns:
                write('  xmlns="%s"\n' % namespace)

Like this, namespaces with a prefix are written in any case. You only check for the empty prefix whether it is already in extra_ns. Maybe you intended the following, which never writes a namespace from extra_ns?

        for prefix, namespace in namespaces:
            # Allow user-provided namespace bindings to prevail
            if prefix not in self.extra_ns:
                if prefix:
                    write('  xmlns:%s="%s"\n' % (prefix, namespace))
                else:
                    write('  xmlns="%s"\n' % namespace)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix-in-progress
Projects
None yet
Development

No branches or pull requests

1 participant