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?

JSON does not have a standard way of representing mixed content.
Different applications come up with different conventions to encode that.

The best I came across so far is JsonML (http://www.jsonml.org/)
Because, each XML element is represented with 2 JS objects - an array for the element, and an object for the attributes.

Another popular representation is Virtual DOM (https://github.com/Matt-Esch/virtual-dom),
which uses 3 JS objects - an object for the element itself, an object for the attributes, and an array for the child nodes.

If you don't mind go beyond JSON syntax, I've invented a new notation Mark (http://marknotation.org/),
which can easily represent both JSON, and XML/HTML data.
And each XML/HTML element is represented with just 1 JS object.

Regards

Henry

On Wed 11/7/18 11:57 PM, Costello, Roger L. wrote:

Hi Folks,

 

I am converting an XML Schema to a JSON Schema.

 

The XML Schema declares an <example> element that has bidirectional-override (BDO) elements intermingled with text, e.g.,

 

<example>Some text <bdo dir="rtl">reverse this text <bdo dir="ltr">not this text</bdo></bdo> finishing text</example>

 

rtl = right-to-left 

ltr = left-to-right 

 

As you can see, the <example> element has mixed content, as does the <bdo> element.

 

At the bottom of this message is the XML Schema.

 

JSON and JSON Schema do not support mixed content, nor do they support attributes.

 

How to represent the XML in JSON and JSON Schema?

 

Here’s what I am thinking: Mixed content is essentially an ordered sequence of text, element, text, element, …

 

JSON arrays are intended to represent ordered sequences. So, I am thinking that mixed content can be represented in JSON with an array. The above <example> element can be expressed this way in JSON:

 

{
  "example": [
      {"text": "Some text "},
      {"bdo":
          [
              {"dir": "rtl"},
              {"text": "reverse this text "},
              {"bdo":
                  [
                     {"dir": "ltr"},
                     {"text": "not this text"}
                  ]
              }
          ]
      },
      {"text": " finishing text"}
  ]
}

 

Notice that the content of the items with mixed content (example and bdo) are represented with arrays.

 

At the bottom of this message is the JSON Schema.

 

Is there a better way to represent mixed content in JSON? What makes one way better than another? One way can be processed easier, more efficiently than another?  /Roger

 

XML Schema

 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:element name="example" type="BDOMixedContent" />
   
    <xs:complexType name="BDOMixedContent" mixed="true">
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="bdo" type="BDOMixedContent" />
        </xs:choice>
        <xs:attribute name="dir">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="ltr" />
                    <xs:enumeration value="rtl" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>

</xs:schema>

 

 

JSON Schema

 

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "BDOMixedContent": {
            "type": "array",
            "items": {
              "anyOf": [
                       {
                           "type": "object",
                           "properties": {
                                 "text": {"type": "string"}
                           },
                           "additionalProperties": false
                       },
                       {
                           "type": "object",
                           "properties": {
                                 "bdo": {"$ref": "#/definitions/BDOMixedContent"}
                           },
                           "additionalProperties": false
                       },
                       {
                           "type": "object",
                           "properties": {
                                 "dir": {"type": "string", "enum": ["ltr", "rtl"]}
                           },
                           "additionalProperties": false
                       }
                ]
            }
        }
    },
    "type": "object",
    "properties": {
          "example": {"$ref": "#/definitions/BDOMixedContent"}
    },
    "required": [ "example" ],
    "additionalProperties": false
}

 

 





[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