question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Complex namespaces

See original GitHub issue

This hope that is not and issue but rather lack of documentation. I have such document:

<response
    xmlns="..."
    xmlns:c="..............."
    xmlns:xml="http://www.w3.org/XML/1998/namespace"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="xxxxxxxx"
>
    <object>
        <name xml:lang="en">Some value</name>
        <name xml:lang="fr">Some value</name>
        <c:contact>
            <c:contactName xml:lang="en">Some value</c:contact-name>
        </c:contact>
    </object>
</response>

This is code snippet that I tried to achieve it but it does not work.

_ns_map = {
    None: "...",
    "xml": "http://www.w3.org/XML/1998/namespace",
    "c": "............"
}

_serializer_config = config = SerializerConfig(
    pretty_print=True,
    schema_location="xxxxxxxx",
)


_serializer = XmlSerializer(config=_serializer_config)

@dataclass
class Contact:
    contact_name: str = field(metadata=dict(type="Attribute", name="contact"))
   
   class Meta:
        namespace = "c"


@dataclass
class Name:
    lang: str = field(metadata=dict(type="Attribute", name="lang"))
    value: str = field(default=None)

    class Meta:
        namespace = "xml"


@dataclass
class Object:
    id: str
    name: List[Name] = field(default_factory=list, metadata={"type": "Element"})
    contact: Contact = field(default=None)

    class Meta:
        namespace = None


@dataclass
class Response:
    object: Object = field(metadata=dict(namespace=None))

    class Meta:
        name = "response"
        namespace = _ns_map[None]

    def render(self) -> str:
        return _serializer.render(self, ns_map=_ns_map)


if __name__ == '__main__':
    print(
        Response(object=Object(
            id="12121",
            name=[Name(lang="en", value="Some value")],
            contact=Contact(contact_name="Some value")
        )).render()
    )

Could you help me to write code that would print proper document structure?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tefracommented, Feb 2, 2021

It’s not necessary, the namespace uri http://www.w3.org/XML/1998/namespace and it’s prefix xml are both reserved and bound by the xml specification and doesn’t need to be declared in the document itself.

1reaction
tefracommented, Feb 2, 2021

Putting it all together

@dataclass
class Contact:
    contact_name: str = field(metadata=dict(type="Attribute", name="contact"))

@dataclass
class Name:
    lang: str = field(metadata=dict(type="Attribute", name="lang", namespace="http://www.w3.org/XML/1998/namespace"))
    value: str = field(default=None)

@dataclass
class Object:
    id: str = field(metadata={"type": "Element"})
    name: List[Name] = field(default_factory=list, metadata={"type": "Element"})
    contact: Contact = field(default=None, metadata={"namespace": _ns_map["c"]})

@dataclass
class Response:
    object: Object = field()

    class Meta:
        name = "response"
        namespace = _ns_map[None]

    def render(self) -> str:
        return _serializer.render(self, ns_map=_ns_map)

<response xmlns="..." xmlns:c="............" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xxxxxxxx">
  <object>
    <id>12121</id>
    <name xml:lang="en">Some value</name>
    <c:contact contact="Some value"/>
  </object>
</response>
``

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Practice: Namespaces in Complex Projects
Working in complex projects or in projects that don't provide a good overview of forthcoming page types and elements may require a defensive ......
Read more >
XSD Namespaces - XML Schema Tutorial - Liquid Technologies
This tutorial explains the use of Namespaces with XML Schema (XSD). ... This article gives an overview of the complex nature of using...
Read more >
Kubernetes architecture: How to use hierarchical namespaces ...
Hierarchical namespaces make it easier to manage individual tenants' permissions and capabilities in a multitenant Kuberentes architecture.
Read more >
XML Schema: Understanding Namespaces - Oracle
Moving to XML Schema? This introduction to namespaces will help you understand one of its more important elements.
Read more >
Adding namespaces, element declarations, and complex ...
Use the namespace pane on the Types tab to add namespaces, element declarations, and complex types to an XML profile.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found