kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.53k stars 81 forks source link

Preserve order of hetergenous elements in output #179

Open kulack opened 5 months ago

kulack commented 5 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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