XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] How to represent mixed content in JSON and JSON Schema?

You need to state your objectives.

One of your objectives, I think, is to map XML to JSON in such a way that every XML document has a JSON representation, and two "distinct" XML documents (in some sense of the term) have distinct JSON representations.

I assume you don't care about the DTD, but you didn't actually say so.

To describe the mapping completely, you need to consider things such as namespaces, comments, processing instructions.

You seem to be using a mapping that requires schema knowledge (different mappings depending on whether an element has mixed content, which can only be known from the schema). Isn't that a rather severe limitation?

You're going to end up with mappings like

<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>

becoming

{"ul": [
    {"li": [{"$": "One"}]},
    {"li": [{"$": "Two"}]},
    {"li": [{"$": "Three"}]}
]}

which some would consider lacking in elegance.... Certainly, the JS code to process this is probably going to be worse than the JS code to handle the XML DOM equivalent.

Michael Kay
Saxonica

On 16 Jul 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?
 
/Roger



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS