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 Children Order in case of mixed content. #317
base: master
Are you sure you want to change the base?
Conversation
In current functionality, in order to maintain Chilren Order in case if mixed content like this: <TailParas fid="td"> <Para>That capitulation reflects the conundrum facing Mr. Ballmer's successor, <En cat="pe" fpRef="958861" instRef="7" djRef="9589153">Satya Nadella</En>, who took over two years ago, as he runs out of options to bolster <En cat="co" fpRef="27147355" instRef="8" djRef="mcrost">Microsoft</En>'s standing in mobile technology. His new strategy aims to help information-technology managers cope with the increasingly central position of smartphones in corporate networks. The company is betting that corporate customers will want to use <En cat="co" fpRef="27147355" instRef="9" djRef="mcrost">Microsoft</En>'s technology to lock down and control devices on their networks.</Para> <Para>In a statement, Mr. Nadella also touted its Continuum feature, which enables a smartphone running Windows 10 to function as a surrogate PC when connected to a monitor and keyboard.</Para> <Para> <En cat="co" fpRef="27147355" instRef="10" djRef="mcrost">Microsoft</En> must establish its Windows operating system on a substantial portion of smartphones or accept a diminished presence in the technology landscape, where mobile devices outnumber personal computers—and it may not get another chance, said Forrester Research Inc. analyst Julie Ask.</Para> </TailParas> you need to pass these flags: explicitChildren: true, preserveChildrenOrder : true, charsAsChildren: true, includeWhiteChars: true And these flag generates lots of extraneous json nodes. With this change I have added a flag called preserveChildrenOrderForMixedContent (default is false). But if it's turned on, it will generate following output. Basically, it's smart enough to detect that it has mixed content and will wrap the object only if required in order to maintain the order. { "$": { "fid": "td" }, "Para": [ { "$$": [ { "_": "That capitulation reflects the conundrum facing Mr. Ballmer\'s successor, " }, { "En": { "_": "Satya Nadella", "$": { "cat": "pe", "fpRef": "958861", "instRef": "7", "djRef": "9589153" } } }, { "_": ", who took over two years ago, as he runs out of options to bolster " }, { "En": { "_": "Microsoft", "$": { "cat": "co", "fpRef": "27147355", "instRef": "8", "djRef": "mcrost" } } }, { "_": "\'s standing in mobile technology. His new strategy aims to help information-technology managers cope with the increasingly central position of smartphones in corporate networks. The company is betting that corporate customers will want to use " }, { "En": { "_": "Microsoft", "$": { "cat": "co", "fpRef": "27147355", "instRef": "9", "djRef": "mcrost" } } }, { "_": "\'s technology to lock down and control devices on their networks." } ] }, "In a statement, Mr. Nadella also touted its Continuum feature, which enables a smartphone running Windows 10 to function as a surrogate PC when connected to a monitor and keyboard.", { "$$": [ { "_": "\\n " }, { "En": { "_": "Microsoft", "$": { "cat": "co", "fpRef": "27147355", "instRef": "10", "djRef": "mcrost" } } }, { "_": " must establish its Windows operating system on a substantial portion of smartphones or accept a diminished presence in the technology landscape, where mobile devices outnumber personal computers—and it may not get another chance, said Forrester Research Inc. analyst Julie Ask." } ] } ] }
Compiled xml2js file for the change I added in corresponding coffee script.
Added Test case for preserveChildrenOrderForMixedContent to improve the test coverage.
Updated Input to cover more conditions
👍 Change was very useful for my use case. Can we merge this with master branch? |
Preserve space between consecutive nodes for mixed content
Preserve space between two consecutive tags.
Preserve space between two consecutive nodes
@Leonidas-from-XIV - Could you please look at the pull request I created and see if there is any possibility of merging this to master ? Thanks, |
@Leonidas-from-XIV I would also need this, as I am trying to process XLIFF (xlf) files generated by angular2 for merging and difference operations. |
@paumayr - If I understand it correctly, you are asking if we can have bidirectional conversion to/from xml to json. Right? It should work. |
@chavdam Awesome... I currently have the following issue: angular2s ng-xi18n generates an xlf file that contains something like this:
I writing tools to deal with such files. ng-xi18n currently always generates localizations for all packages, including dependencies. Now, once I load and store such a file, it reorders mixed content like this (this time including the translation node):
So obviously it causes confusion in the UI with the mixed-up text. |
@paumayr - Did you test using my patch to see if it satisfies your requirement ? |
@chavdam sorry for the late (really late) reply. I just tested your patch, and I think it works for parsing. But I think the builder requires support for converting $$ back to text, by only printing the content of $$ nodes. source:
The result is the following (with pretty printing)
When disabling pretty-printing and removing <$$> and </$$> tags using something like this after writing I get the results I need:
soo, almost :).. Could we add an option to write the content of a specific element tag without writing the element tags? |
In current functionality, in order to maintain Chilren Order in case if mixed content like this:
you need to pass these flags:
explicitChildren: true, preserveChildrenOrder : true, charsAsChildren: true, includeWhiteChars: true
And these flag generates lots of extraneous json nodes.
With this change I have added a flag called preserveChildrenOrderForMixedContent (default is false). But if it's turned on, it will generate following output. Basically, it's smart enough to detect that it has mixed content and will wrap the object only if required in order to maintain the order.