highsource / jsonix

Powerful XML<->JSON JavaScript mapping library.
BSD 2-Clause "Simplified" License
361 stars 78 forks source link

CDATA support #214

Open Programmierschmied opened 5 years ago

Programmierschmied commented 5 years ago

Dear developers,

I have to use mixed CDATA contents. It means that the node can have CDATA or text or both mixed. I am not able to change the XSD Schema for a new simply type. For a fast solution I added a regex detection for the use of cdata in the jsonix.js, but I don't think that this is a good solution. Is there a way to keep CDATA from XML file?

My fast solution: if (this.asCDATA && value.match(/<.+>/g) != null)

Best regards, Justin

highsource commented 5 years ago

Could you please give an example of XMLs an how you‘d like them to be represented in JS. I‘m a bit puzzled as CDATA is just character data. So this should actually work OTOB, not sure why special support is needed.

Am 14.03.2019 um 13:50 schrieb Justinizer notifications@github.com:

Dear developers,

I have to use mixed CDATA contents. It means that the node can have CDATA or text or both mixed. I am not able to change the XSD Schema for a new simply type. For a fast solution I added a regex detection for the use of cdata in the jsonix.js, but I don't think that this is a good solution. Is there a way to keep CDATA from XML file?

My fast solution: if (this.asCDATA && value.match(/<.+>/g) != null)

Best regards, Justin

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

Programmierschmied commented 5 years ago

For example: <content lang="de"><![CDATA[<p>Something<latex>$m \ge 1 g$</latex> text.</p>]]></content>

if i convert it to js and back it gets to: <content lang="de">&lt;p&gt;Something &lt;latex&gt;$m \ge 1 g$&lt;/latex&gt; text.&lt;/p&gt;<content>

Edit: If I turn CDATA on for this simpletype then every node gets CDATA. Even these which hadn't CDATA before.

Programmierschmied commented 5 years ago

Sorry to did bother you.

I have researched in other XML to objects Parser. To mix CDATA with normal content is a mistake by XSD Design. I will please the writer of xsd file to change this.

Thank you for your fast support!

highsource commented 5 years ago

I'm on FOSSGIS2019 so I can't reply promptly.

Technically this

<content lang="de"><![CDATA[<p>Something<latex>$m \ge 1 g$</latex> text.</p>]]></content>

is exactly the same as this

<content lang="de">&lt;p&gt;Something &lt;latex&gt;$m \ge 1 g$&lt;/latex&gt; text.&lt;/p&gt;<content>

I understand that these are different cosmetics but technically it is not really relevant.

However I can follow that you might be interested in actual cosmetics to make XML more readable. The problem is, however the presentation in the JS side. Somehow you will need to distinguish between normal text and CDATA-text. So improving cosmetics on the XML side might lead to much more complex models on the JS side. Which, in my PoV is a too high price.

But here's an idea. Maybe we could implement some automatic that Jsonix automatically produces CDATA sections if the text contains too much markup/special characters? Like if there are more that N &lt;s, &gt;s, &apos;es and &amp;s then automatically create a CDATA-section.
What do you think?

Programmierschmied commented 5 years ago

I think this is a really good idea. Then we got a feature which other parsers like JAXB don't got. I just found out that this is a general parser problem.

Programmierschmied commented 5 years ago

I reopen it. So we can find a solution for. Should I write some code and do a merge request. Or do you want to write code yourself?

Programmierschmied commented 5 years ago

I just had an idea. Maybe you like: https://github.com/highsource/jsonix/pull/215

This would allow the user to do a conditional CDATA by regex.

if ((this.asCDATA && this.CDATAcondition == null) || (this.asCDATA && value.match(new RegExp(this.CDATAcondition)) ))