Skip to content

Default namespace and to_xml() adds ns0 tags everywhere. #27

@jsenecal

Description

@jsenecal

Hello there,

When using nsmap with a default namespace and using "to_xml" method, ns0: is prepended to every xml attribute. as such:

Python 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from typing import List, Optional, Literal

In [2]: from pydantic_xml import BaseXmlModel, attr, element, wrapped

In [3]: from pydantic import HttpUrl

In [4]: class MultiLineElement(BaseXmlModel):
   ...:     number: int = attr()  # type: ignore
   ...:     line: str
   ...: 

In [5]: class PocLinkRef(BaseXmlModel):
   ...:     description: Literal["Admin", "Tech", "Routing"] = attr()  # type: ignore
   ...:     function: Literal["AD", "T", "R"] = attr()  # type: ignore
   ...:     handle: str = attr()  # type: ignore
   ...: 

In [6]: class Org(BaseXmlModel, tag="org", nsmap={"": "http://www.arin.net/regrws/core/v1"}):
   ...:     street_address: List[MultiLineElement] = wrapped('streetAddress', element(tag="line") ) # type: ignore
   ...:     city: str = element()  # type: ignore
   ...:     iso3166_2: Optional[str] = element(tag="iso3166-2")  # type: ignore
   ...:     postal_code: Optional[str] = element(tag="postalCode")  # type: ignore
   ...:     comment: Optional[List[MultiLineElement]]  = wrapped('comment', element(tag="line") )# type: ignore
   ...: 
   ...:     handle: Optional[str] = element()  # type: ignore
   ...:     registration_date: Optional[str] = element(tag="registrationDate")  # type: ignore
   ...:     org_name: str = element(tag="orgName")  # type: ignore
   ...:     dba_name: Optional[str] = element(tag="dbaName")  # type: ignore
   ...:     tax_id: Optional[str] = element(tag="taxId")  # type: ignore
   ...:     org_url: Optional[HttpUrl] = element(tag="orgUrl")  # type: ignore
   ...: 

In [7]: PAYLOAD = """
   ...:     <org xmlns="http://www.arin.net/regrws/core/v1" >
   ...:         <streetAddress>
   ...:             <line number = "1">Line 1</line>
   ...:         </streetAddress>
   ...:         <city>Chantilly</city>
   ...:         <iso3166-2>VA</iso3166-2>
   ...:         <postalCode>20151</postalCode>
   ...:         <comment>
   ...:             <line number = "1">Line 1</line>
   ...:         </comment>
   ...:         <registrationDate>Mon Nov 07 14:04:28 EST 2011</registrationDate>
   ...:         <handle>ARIN</handle>
   ...:         <orgName>ORGNAME</orgName>
   ...:         <dbaName>DBANAME</dbaName>
   ...:         <taxId>TAXID</taxId>
   ...:         <orgUrl>http://example.com/org/ARIN</orgUrl>
   ...:     </org>
   ...:     """

In [8]: org = Org.from_xml(PAYLOAD)

In [9]]: print(org.to_xml().decode())
<ns0:org xmlns:ns0="http://www.arin.net/regrws/core/v1" xmlns="http://www.arin.net/regrws/core/v1"><ns0:streetAddress><ns0:line number="1">Line 1</ns0:line></ns0:streetAddress><ns0:city>Chantilly</ns0:city><ns0:iso3166-2>VA</ns0:iso3166-2><ns0:postalCode>20151</ns0:postalCode><ns0:comment><ns0:line number="1">Line 1</ns0:line></ns0:comment><ns0:handle>ARIN</ns0:handle><ns0:registrationDate>Mon Nov 07 14:04:28 EST 2011</ns0:registrationDate><ns0:orgName>ORGNAME</ns0:orgName><ns0:dbaName>DBANAME</ns0:dbaName><ns0:taxId>TAXID</ns0:taxId><ns0:orgUrl>http://example.com/org/ARIN</ns0:orgUrl></ns0:org>

Payload:

<org xmlns="http://www.arin.net/regrws/core/v1">
  <streetAddress>
    <line number="1">Line 1</line>
  </streetAddress>
  <city>Chantilly</city>
  <iso3166-2>VA</iso3166-2>
  <postalCode>20151</postalCode>
  <comment>
    <line number="1">Line 1</line>
  </comment>
  <registrationDate>Mon Nov 07 14:04:28 EST 2011</registrationDate>
  <handle>ARIN</handle>
  <orgName>ORGNAME</orgName>
  <dbaName>DBANAME</dbaName>
  <taxId>TAXID</taxId>
  <orgUrl>http://example.com/org/ARIN</orgUrl>
</org>

Result:

<ns0:org xmlns:ns0="http://www.arin.net/regrws/core/v1" xmlns="http://www.arin.net/regrws/core/v1">
  <ns0:streetAddress>
    <ns0:line number="1">Line 1</ns0:line>
  </ns0:streetAddress>
  <ns0:city>Chantilly</ns0:city>
  <ns0:iso3166-2>VA</ns0:iso3166-2>
  <ns0:postalCode>20151</ns0:postalCode>
  <ns0:comment>
    <ns0:line number="1">Line 1</ns0:line>
  </ns0:comment>
  <ns0:handle>ARIN</ns0:handle>
  <ns0:registrationDate>Mon Nov 07 14:04:28 EST 2011</ns0:registrationDate>
  <ns0:orgName>ORGNAME</ns0:orgName>
  <ns0:dbaName>DBANAME</ns0:dbaName>
  <ns0:taxId>TAXID</ns0:taxId>
  <ns0:orgUrl>http://example.com/org/ARIN</ns0:orgUrl>
</ns0:org>

Is that intended and is there a way around this?

With many thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions