Re: [xml-dev] Best options for converting JSON to XML

From
Michael Kay <>
To
David Lee <>
Date
2013-11-10T18:33:55Z
ID
<>
Thread
Re: [xml-dev] Best options for converting JSON to XML
> 
> { "a json name" : 1 }                ->
> 
> <object>
>    <member name="a json name">< value type="Number">123</value></<member>
> </object>
> 
> 
> "For every member element with an attribute named 'name' with a value "a json name" it must have a single child 
> element "value" with a "type" attribute  with the value "Number" and its text contents must follow the constraints of xs:decimal
> 
> This is difficult or impossible to express in common XML schema languages 
> (I think you could do it in schematron though ... painfully,  maybe WC3 schema 1.1 with assertions ?)
> 


I don't think it's that painful in XSD 1.1 with conditional type assignment (well, no more painful than anything else in XSD, anyway):

<xs:element name="member">
    <xs:alternative test="@name='a json name'" type="jsonNumber"/>
    <xs:alternative test="@name='another json name'" type="jsonBoolean"/>
    ....
 </xs:element>

<xs:complexType name="jsonNumber">
  <xs:sequence>
   <xs:element name="Value">
     <xs:complexType>
       <xs:simpleContent>
         <xs:extension base="xs:double">
           <xs:attribute name="type" fixed="Number"/>

Michael Kay
Saxonica