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

Preserve order of hetergenous elements in output #179

Open
kulack opened this issue Feb 6, 2024 · 3 comments
Open

Preserve order of hetergenous elements in output #179

kulack opened this issue Feb 6, 2024 · 3 comments

Comments

@kulack
Copy link

kulack commented Feb 6, 2024

Hi,
I have an XML file in which the ordering of the elements is important. When I translate it to Json, I'd like to get the elements in a single list. Is there a way to do this in xq? I can't quite figure out a way.

For example, in the file

<root>
  <a item="1"/>
  <b item="2"/>
  <a item="3"/>
  <b item="4"/>
</root>

In the actual document the item attribute representing the order is not present and its only the order in the XML schema that controls the order that the elements should be processed in.

xq generates this output for this input:

{
  "root": {
    "a": [
      {
        "@item": "1"
      },
      {
        "@item": "3"
      }
    ],
    "b": [
      {
        "@item": "2"
      },
      {
        "@item": "4"
      }
    ]
  }
}

This is great and expected, but my particular use case uses the element name to distinguish only slight differences in element contents and I need to preserve the order between all a and b elements so I can process them all in order.

Is there a way to represent this in xq output?

@kislyuk
Copy link
Owner

kislyuk commented Mar 1, 2024

Hi, sorry I have no idea how to do this. We use xmltodict (https://github.com/martinblech/xmltodict) to transform XML to JSON, and I'm not aware of a way to do this in xmltodict. If you're able to demonstrate a way to do this in that library, it's possible we could add a configuration option for that.

@kulack
Copy link
Author

kulack commented Mar 1, 2024

Yeah, I figured it was something like that and seems like a common pattern. I really appreciate the response. I'll keep my eye open if I find anything fun I'll post back.
Thanks for the great utility!

@pyhedgehog
Copy link

There are discussion related to this issue in xmldict#247 and xmldict#340. Not sure these ideas will be adopted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants