tags:

views:

17

answers:

1

I am implementing a RESTful web service that currently handles JSON and needs to handle XML. I use Jackson to automatically marshall Java objects to and from JSON. For XML I need to define a Schema, mainly because a customer wants it. With luck, I can hand the Schema to JAXB and not have to code the XML marshalling myself.

One feature is that the client can set "custom properties" on an entity. These are stored but not interpreted by the server; they are for the client program's convenience. Example in JSON:

{"id":"abcde",
 "customProperties":{
    "foo":"bar", "rating":5,
    "ridiculousExample":{"food":["green eggs","ham"],
             "innerObject":{"name":"Bill","age":47}}
  }
}

Presumably in XML this would look like:

<whatever>
  <id>abcde</id>
  <customProperties>
    <customProperty>
      <foo>bar</foo>
    </customProperty>
    <customProperty>
      <rating>5</rating>
    </customProperty>
    <customProperty>
      <ridiculousExample>
        <food>
          <foodItem>green eggs</foodItem>
          <foodItem>ham</foodItem>
        <food>
        <innerObject>
          <name>Bill</name>
          <age>47</age>
        </innerObject>
      </ridiculousExample>
    </customProperty>
  </customProperties>
</whatever>

Internally (in Java) a JSON array is just an array, and a JSON object is a HashMap. We can generate XML from arrays of hashmaps of arrays by following rules, allowing us to translate these custom properties between XML and JSON.

Is there any way in XML Schema to specify things down to a certain point and say "from here on down it's open-ended as long as it's valid XML"?

+2  A: 

Yes, just use any:

<xs:element name="customProperty">
    <xs:complexType>
        <xs:sequence>
            <xs:any namespace="##any"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
xcut