json2xml icon indicating copy to clipboard operation
json2xml copied to clipboard

List items with attributes

Open Jeroendevr opened this issue 2 years ago • 10 comments

Concept issue

Is your feature request related to a problem? Please describe. When creating a xml from a list and wanting to add attributes to it seems impossible.

Describe the solution you'd like Something that should result in

<transportation-mode xml:lang="nl">Fiets</transportation-mode>
<transportation-mode xml:lang="nl">Bus</transportation-mode>
<transportation-mode xml:lang="en">Bike</transportation-mode>

Describe alternatives you've considered

  • [ ] A clear and concise description of any alternative solutions or features you've considered.

Option 1 - Multiple sets of attrs and values after each other

{
  'transportation-mode' : {
      '@attrs': {'xml:lang': 'nl'}, '@val': ['Fiets', 'Bus'],
      '@attrs': {'xml:lang': 'en'}, '@val': ['Bike']
  }
}

Option 2

{
  'transportation-mode' : { 
      ['@attrs': {'xml:lang': 'nl'}, '@val': ['Fiets', 'Bus'] ],
      ['@attrs': {'xml:lang': 'en'}, '@val': ['Bike'] ]
  }
}

Option 3 - For every value an attr with values as list

{
  'transportation-mode' : {
      '@attrs': {'xml:lang': 'nl'}, '@attrs': {'xml:lang': 'nl'}, 
      '@val': ['Fiets', 'Bus'],
      '@attrs': {'xml:lang': 'en'}, 
      '@val': ['Bike']
  }
}

Option 4 - For every value an attr as a sequence in a list

{
  'transportation-mode' : { [
        '@attrs': {'xml:lang': 'nl'} '@val': 'Fiets', 
        '@attrs': {'xml:lang': 'nl'} '@val': 'Bus',
        '@attrs': {'xml:lang': 'en'} '@val': 'Bike'
  ] }
}

Option 5 - For every value an attr as a sequence in a listv2

{
  'transportation-mode' : [
        {
            '@attrs': {'xml:lang': 'nl'},
            '@val': 'Fiets'
        },
        {        
            '@attrs': {'xml:lang': 'nl'},
            '@val': 'Bus'
        },
        {
            '@attrs': {'xml:lang': 'en'},
            '@val': 'Bike'
        }  
   ]
}

Additional context It sounds similar to issue #146 but I don't want to hijack that issue to promote my own feature request. @Estherfranssen could you confirm this would be a workable solution?

Edit jan-2023 Updated solution I'd like

Jeroendevr avatar Dec 16 '22 19:12 Jeroendevr

Yes, thanks this would suit for what I want to achieve

EstherFranssen avatar Dec 19 '22 09:12 EstherFranssen

After consideration, it is not completely what I need.

Below a possible solution:

{ 
  'searchword' : [
      {'@attrs': {'xml:lang': 'nl'}, '@val': 'Adembescherming'},
      {'@attrs': {'xml:lang': 'en'}, '@val': 'Breathing protection'}
  ]
}

Which should result in:

<searchword xml:lang="nl">Adembescherming</searchword>
<searchword xml:lang="en">Breathing protection</searchword>

I hope this helps

EstherFranssen avatar Dec 20 '22 12:12 EstherFranssen

{ 
  "vehicle" : {
      "attrs": "transportation-mode", 
      "val": ["Bike", "Bus", "Tram"]
  }
}

may be converted to

<?xml version="1.0" encoding="UTF-8"?>
<vehicle>
  <attrs>transportation-mode</attrs>
  <val>Bike</val>
  <val>Bus</val>
  <val>Tram</val>
</vehicle>

javadev avatar Jan 02 '23 14:01 javadev

@javadev what do you mean with your comment? An example of the code behaviour at the moment?

Jeroendevr avatar Jan 03 '23 09:01 Jeroendevr

Too many implementation possibilities 🤨. Although I have yet to find out which could be implemented at all, I don't have a strong preference yet. @vinitkumar Could you give your opinion about a possible implementation 🧐. You possibly have a better understanding about the feasibility.

Jeroendevr avatar Jan 03 '23 14:01 Jeroendevr

I see @EstherFranssen That your suggestion is similar to option 5 so I think I'll make an attempt at it.

Jeroendevr avatar Jan 03 '23 14:01 Jeroendevr

Guys, thanks for all the comments and for providing implementation ideas. I will look into each of these and give my opinion by this weekend, and possibly a PR for it as well.

On Tue, 3 Jan 2023 at 19:53, Jeroendevr @.***> wrote:

I see @EstherFranssen https://github.com/EstherFranssen That your suggestion is similar to option 5 so I think I'll make an attempt at it.

— Reply to this email directly, view it on GitHub https://github.com/vinitkumar/json2xml/issues/163#issuecomment-1369826554, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEDITSKZB4RKZNH36USLKTWQQY7PANCNFSM6AAAAAATBOGOZ4 . You are receiving this because you were mentioned.Message ID: @.***>

-- Thanks and Regards, Vinit Kumar

vinitkumar avatar Jan 03 '23 14:01 vinitkumar

Nice, awesome. I have added a PR with a unit test if we are going for option 5. Let me know if I can do anything else for you.

Jeroendevr avatar Jan 03 '23 15:01 Jeroendevr

@EstherFranssen A new release of json2xml has been released and thanks to the great work of @Jeroendevr, we have list with attributes support live.

Please grab the latest code from https://json2xml.readthedocs.io/en/latest/, https://pypi.org/project/json2xml/3.21.0/ and see if it solves your use case. If there are some bugs, please create a ticket.

vinitkumar avatar Jan 18 '23 15:01 vinitkumar

Thanks @vinitkumar, @Jeroendevr, this is great!

EstherFranssen avatar Jan 20 '23 10:01 EstherFranssen