[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: RE: How to represent mixed content in JSON and JSON Schema
On 16 July 2018 at 19:56, Costello, Roger L. <costello@mitre.org> wrote: > Hi Folks, > > > > I have examined several approaches that people have developed to represent > mixed content in JSON. I have identified their best features. > > > > I created an approach that incorporates all their best features. > > > > Below is my recommended approach for representing mixed content in JSON. I > welcome your comments and critiques. > > > > I will illustrate my approach by showing how to convert the following XML > containing mixed content to JSON: > > > > <example>Some text <bdo dir="rtl">reverse this text <bdo dir="ltr">not this > text</bdo></bdo> finishing text</example> > > > > Rule #1: Represent each XML element as a JSON object property, where the > property name is the element name. > > > > The <example> element is represented as this JSON object property: > > > > "example": … > > > > Rule #2: Represent each XML text node as a JSON object property, where the > property name is $ and the property value is a string corresponding to the > string value of the text node. > > > > The text node within this <bdo> element: > > > > <bdo dir="ltr">not this text</bdo> > > > > is represented by this JSON property/value pair: > > > > "$": "not this text" > > > > Rule #3: Represent each XML attribute/value pair as a JSON object > property/value pair, where the property name is the attribute name preceded > by the @ symbol and the property value is a string corresponding to the > string value of the attribute value. > > > > The dir=”ltr” within this <bdo> element: > > > > <bdo dir="ltr">not this text</bdo> > > > > is represented by this JSON property/value pair: > > > > "@dir": "ltr" > > > > Rule #4: If an element’s content is not mixed (mixed=”false”), then > represent its content as a JSON object: > > > > The following <bdo> element does not have mixed content: > > > > <bdo dir="ltr">not this text</bdo> > > > > Applying Rules 1-4 yields this JSON: > > > > "bdo": { > "@dir": "ltr", > "$": "not this text" > } > > > > Rule #5: If an element has mixed content, then represent the element’s > content (including attribute/value pairs) in JSON as an array, where each > item is embedded in a JSON object. The attributes must be the first items in > the array. The remaining JSON array items must be in the same order as they > are in the XML mixed content. > > > > Per Rule #3 attribute/value pairs are represented as JSON object > property/value pairs. Per this rule, embed each property/value pair within a > JSON object. > Per Rule #2 text nodes are represented as JSON object property/value pairs. > Per this rule, embed each property/value pair within a JSON object. > Per Rule #1 elements are represented as JSON object property/value pairs. > Per this rule, embed each property/value pair within a JSON object. > > > > The outer <bdo> element has mixed content: > > > > <bdo dir="rtl">reverse this text <bdo dir="ltr">not this text</bdo></bdo> > > > > Represent it in JSON this way: > > > > "bdo": [ > {"@dir": "rtl"}, > {"$": "reverse this text"}, > {"bdo": { > "@dir": "ltr", > "$": "not this text" > } > }, > {"$": "finishing text"} > ] > > > > Rule #6: If an element is the root element, embed it in a JSON object. > > > > The <example> element is the root element, so it is embedded in a JSON > object: > > > > {"example": [ > {"$": "some text "}, > {"bdo": [ > {"@dir": "rtl"}, > {"$": "reverse this text"}, > {"bdo": { > "@dir": "ltr", > "$": "not this text" > } > }, > {"$": "finishing text"} > ] > } > ] > } > > > > Read as: This JSON represents an <example> element which has mixed content. > The mixed content contains text (some text), followed by a <bdo> element, > followed by text (finishing text). The outer <bdo> element contains mixed > content and an attribute dir with value rtl. Its mixed content is text > (reverse this text) followed by an inner <bdo> element. The inner <bdo> > element contains text (not this text) and an attribute dir with value ltr. > > > > Comments? seems somewhat underspecified:-) you seem to be using "does not have mixed content" to mean "has a single child, which is text" but mixed content usually means has a mix of text and element nodes, so an element with just element children is not considered mxed content. but also <foo> x<!--abc-->y</foo> is this represented as having a single text node, or two? <foo xmlns="">x</x</foo> is this represented as having 1 attribute or none? is <foo><![CDATA[x]]></foo> represented the same way as <foo>x</foo> is <foo> <x/> <y/> </foo> represented the same way as <foo><x/><y/></foo> is <foo>A</foo> the same as <foo>A</foo> David > > > > /Roger
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] |
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|