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
xml2js does not preserve element order within a level #31
Comments
I guess I need to add an additional option somewhere around line 92 in Fancy, the original xml2js output style was broken in so many ways, there's really a lot of border-cases prohibiting sane parsing. |
Another difficult is that if you use namespace prefixes, you get property keys that must be strings because of the colon: xml:output ----> {"xml:output" :"x"} rather than just {output : "x"} This isn't a big deal, but it means you code a lot of ugly element["xml:output"] rather than element.output We coded up a quick SAX parser that his this basic model:
I'm almost tempted to tweak it to:
|
Any chance order preservation might be added in the near future? Would love to see it in the library (without, ahem, necessarily teaching myself coffeescript) |
Well, not before 0.2 release, sorry. I plan to rewrite it a little and keep things like this in mind but time and motivation are a bit of a problem. Oh and sorry for replying so late, I saw your comment but somehow it slipped my mind to answer! |
No worries, I think your library is great. Would it help if I could raise On Mon, Apr 30, 2012 at 10:49 PM, Marek Kubica <
|
Not sure. I'll try to push 0.2 as soon as possible and then start thinking about fixing this problem. The way it is done currently is rather ugly and hackish, rewriting will make the code way clearer. |
Looks like following issue has the same background:
translates to
This case is more painful than squashing elements, because all text nodes are merged into one string instead of array of strings at least. |
I agree with @kaero, that is really painful :( |
👍 Yep, one entity for all text nodes looks not good. |
Yes. Some parsers handle it by having a tail attribute but I think it is rather confusing. Boy I wish I had the time to start a proper 0.3 branch and rewrite the whole ugly element mess, maybe even on top of some other parser library. |
+1 |
I'm parsing SVG where node order matters. xamel does a good job and is easy to use. +1 on xamel. |
Looking really good. But I wish it would preserve the order. Would it be a major re-write?--I haven't looked through the code yet. |
Well, I did plan a rewrite in 0.3, but that kinda didn't work out due to time limitations. My plan was to use htmlparser2 which is faster and already provides a xml2js-like object that could be transformed to the easy to use object that xml2js provides. Maybe I'll be able to do this with xml2js 0.5, contributions are welcome :-) |
Also need that order despertly. anyone has an idea how to do it ? |
thanks, i'll try it |
Just ran into this issue trying to parse svg documents and I was losing my mind. thanks for the heads up on xamel. |
Any update on this issue? This is a very common issue in a lot of XML docs. I would think the solution here could be as simple as changing the object model to add a |
Currently no work is being done, but if you feel like submitting a PR that changes the current behaviour to something more sane (a.k.a preserving the order), I'd definitely be interested. |
OK, I'll make a fork and take a look at the code to see if I can come up with something. No promises of course :) |
OK, so, the above PR provides us with what I view is a viable workaround for this issue. In this configuration the parser behaves like normal (still collects nodes in named properties for convenience) with the "children" field now becoming and ordered array. The nodes all keep the Note: both |
@ryedin @Leonidas-from-XIV Is it already published in the latest release, i.e. |
@apendua As the README says, these options are only available starting with |
@apendua if you need the feature right away, you can change your |
@ryedin Thanks for response. I was confused because for some reason I thought that your latest PR ended up in This said I also thought |
The I released |
While rebuilding this ordered js to xml, the builder doesn't rebuild it in the ordered form. The order gets messed up again while building it back. Is there any workaround for that ? |
This is still an issue. "At the moment, a one to one bi-directional conversion is guaranteed only for default configuration" (from the README) is therefore not correct. |
Yes, I should remove the note about that for now until I have time to rewrite it. |
It is also weird that it is still the default behavior to mess up the order. That is not what one expects an XML parser does, because in XML order matters. A bit of an annoying gotcha to default to something that doesn't make sense with XML. |
… containers and single items, e.g. podcasts and radio stations. See Leonidas-from-XIV/node-xml2js#31 for more information.
Here's the input XML
And here's what xml2js reads in:
The key thing to notice is that in the xml its:
Originally the order was:
but in the result, it's more like:
it basically combines all tags with the same element name at a particular level, and in so doing, BLOWS away the (XML proscribed) preservation of tag order. And there's no way to re-establish the original order.
So if you tried to represent your XHTML with this, all your p, h, a and such tags would all scrunched with identical tags
Note: I'm not trying to read in XHTML - i just used those tags as an example of why it might be a problem for some schemas.
xml2js does have this sexy sounding option:
While it does indeed "put child nodes in an array", it doesn't preserve the document order.
The text was updated successfully, but these errors were encountered: