UML into JSON Schema and XSD

≯℡__Kan透↙ 提交于 2019-12-12 06:07:21

问题


I created simple UML diagram which is part of bigger diagram of cinema.

Image

Based on this diagram I created Json Schema and XSD files

Json Schema:

-> movieSchema.json

    {
    "type": "object",
    "required": true,
    "properties": {
        "idOfMovie": {
            "type": "integer",
            "required": true
        },
        "title": {
            "type": "string",
            "required": true
        },
        "is3D": {
            "type": "boolean",
            "required": true
        },
        "yearOfProduction": {
            "type": "integer",
            "required": true
        },
        "ageCategory": {
            "type": "integer",
            "required": true
        },
        "description": {
            "type": "string",
            "required": true
        }
    } 
}

-> emissionSchema.json

 {
    "type": "object",
    "required": true,
    "properties": {
        "idOfEmission": {
            "type": "integer",
            "required": true
        },
        "idOfMovie": {
            "type": "integer",
            "required": true
        },
        "dateOfEmission": {
            "type": "string",
            "required": true
        },
        "idOfHall": {
            "type": "integer",
            "required": true
        }
    }
}

And XSD:

->movie.xsd

    <?xml version="1.0" encoding="UTF-8"?>

<schema version="1.0"
           xmlns="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:x-test" xmlns:tst='urn:x-test' 
           elementFormDefault="qualified">

    <element name="movie" type="tst:movieType"/> 
    <complexType name="movieType">
        <sequence>
            <element name="title" type="string" use="required" />
            <element name="is3D" type="boolean" use="required" />
            <element name="yearOfProduction" type="integer" use="required" />
            <element name="ageCategory" type="integer" use="required" />
            <element name="description" type="string" use="required" />
        </sequence>
        <attribute name="idOfMovie" type="integer" use="required"/>
    </complexType>

    <element name="movies" type="tst:moviesType"/> 
    <complexType name="moviesType">
        <sequence minOccurs="0" maxOccurs="unbounded">
            <element ref="tst:movie"/>
        </sequence>
    </complexType>
</schema>

->emission.xsd

<?xml version="1.0" encoding="UTF-8"?>

<schema version="1.0"
           xmlns="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:x-test" xmlns:tst='urn:x-test' 
           elementFormDefault="qualified">

    <element name="emission" type="tst:emissionType"/> 
    <complexType name="emissionType">
        <sequence>
            <element name="idOfMovie" type="integer" use="required" />
            <element name="dateOfEmission" type="string" use="required" />
            <element name="idOfHall" type="integer" use="required" />
        </sequence>
        <attribute name="idOfEmission" type="integer" use="required"/>
    </complexType>

    <element name="emissions" type="tst:emissionsType"/> 
    <complexType name="emissionsType">
        <sequence minOccurs="0" maxOccurs="unbounded">
            <element ref="tst:emission"/>
        </sequence>
    </complexType>
</schema>

And files with data:

-->movies.json

    [
    {
        "idOfMovie": 1,
        "title": "...",
        "is3D": TRUE,
        "yearOfProduction": 2015,
        "ageCategory": 7,
        "description": "..."
    },
    {
        "idOfMovie": 2,
        "title": "...",
        "is3D": TRUE,
        "yearOfProduction": 2015,
        "ageCategory": 12,
        "description": "..."
    },
    {
        "idOfMovie": 3,
        "title": "...",
        "is3D": FALSE,
        "yearOfProduction": 2015,
        "ageCategory": 12,
        "description": "..."
    }
]

--> emissions.json

    [
    {
        "idOfEmission": 1,
        "idOfMovie": 1,
        "dateOfEmission": "01-03-2015-14:00",
        "idOfHall":  2
    },
    {
        "idOfEmission": 2,
        "idOfMovie": 1,
        "dateOfEmission": "02-03-2015-14:00", 
        "idOfHall":  2
    },
    {
        "idOfEmission": 3,
        "idOfMovie": 3,
        "dateOfEmission": "01-03-2015-13:30",
        "idOfHall":  1
    }
]

-->movies.xml

    <mov:movies
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:mov='urn:x-test'
    xsi:schemaLocation='urn:x-test movie.xsd'>

    <mov:movie idOfMovie="1">
        <mov:title>...</mov:title>
        <mov:is3D>TRUE</mov:is3D>
        <mov:yearOfProduction>2015</mov:yearOfProduction>
        <mov:ageCategory>7</mov:ageCategory>
        <mov:description>...</mov:description>
    </mov:movie>
    <mov:movie idOfMovie="2">
        <mov:title>...</mov:title>
        <mov:is3D>TRUE</mov:is3D>
        <mov:yearOfProduction>2015</mov:yearOfProduction>
        <mov:ageCategory>12</mov:ageCategory>
        <mov:description>...</mov:description>
    </mov:movie>
    <mov:movie idOfMovie="3">
        <mov:title>...</mov:title>
        <mov:is3D>FALSE</mov:is3D>
        <mov:yearOfProduction>2015</mov:yearOfProduction>
        <mov:ageCategory>12</mov:ageCategory>
        <mov:description>...</mov:description>
    </mov:movie>
</mov:movies>

-->emissions.xml

    <emi:emissions
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:emi='urn:x-test'
    xsi:schemaLocation='urn:x-test emission.xsd'>

    <emi:emission idOfEmission="1">
        <emi:idOfMovie>1</emi:idOfMovie>
        <emi:dateOfEmission>01-03-2015-14:00</emi:dateOfEmission>
        <emi:idOfHall>2</emi:idOfHall>
    </emi:emission>
    <emi:emission idOfEmission="2">
        <emi:idOfMovie>1</emi:idOfMovie>
        <emi:dateOfEmission>02-03-2015-14:00</emi:dateOfEmission>
        <emi:idOfHall>2</emi:idOfHall>
    </emi:emission>
    <emi:emission idOfEmission="3">
        <emi:idOfMovie>3</emi:idOfMovie>
        <emi:dateOfEmission>01-03-2015-13:30</emi:dateOfEmission>
        <emi:idOfHall>1</emi:idOfHall>
    </emi:emission>
</emi:emissions>

I heard from my teacher that into schemas and files with data, types Movie and Emission arent related properly, different than in UML diagram, but I dont know how to repair this, I thought that it is OK.


回答1:


What you need it a model for the relation between both - the association. You would need some kind of association class that you could call MovieEmissions where you put in the id of a Movie and those of the Emissions.




回答2:


In the image you posted, you have the idOfMovie:int attribute in both Movie and Emission feature. You probably wanted to use this key to link them? This is actually the way linking is done in the (relational) database world. Where you link two tables by a common key. You have designed your features as if they were db tables.

In UML that you want to map to XML, give the association a name and drop the ids. Mapped to XML you get something like this:

<Movie>
  <title> 
    ..
  </title>
  <..>
  </..>
  <Emission>
    <date> .. </..>
    <hall> .. </..>
  <Emission>
  <Emission>
    <date></..>
    <hall></..>
  <Emission>

</Movie>

You can also just use a single XSD for both features



来源:https://stackoverflow.com/questions/28574879/uml-into-json-schema-and-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!