Ограничение целостности в схеме XML - PullRequest
1 голос
/ 20 ноября 2011

У меня есть такой XML-код (рабочий процесс представляет собой своего рода класс, экземпляр которого создается процессом).

<workflowManagementSystem>
    <workflows>
        <workflow name="workflow1">
            <actions>
                <action name="action1" role="role1"></action>
                <action name="action2" role="role3"></action>
                <action name="action3" role="role4"></action>
            </actions>
        </workflow>
        <workflow name="workflow2">
            <actions>
                <action name="action3" role="role4"></action>
                <action name="action2" role="role3"></action>
                <action name="action4" role="role4"></action>
            </actions>
        </workflow>
    </workflows>
    <actors>
        <actor name="actor1" role="role1"></actor>
        <actor name="actor2" role="role2"></actor>
        <actor name="actor3" role="role3"></actor>
        <actor name="actor4" role="role4"></actor>
        <actor name="actor5" role="role2"></actor>
    </actors>
    <processes>
        <process workflow="workflow1">
            <actionStatuses>
                <actionStatus action="action1" actor="actor1"></actionStatus>
                <actionStatus action="action2" actor="actor3"></actionStatus>
            </actionStatuses>
        </process>
        <process workflow="workflow1">
            <actionStatuses>
                <actionStatus action="action1" actor="actor1"></actionStatus>
                <actionStatus action="action2" actor="actor5"></actionStatus>
                <actionStatus action="action3" actor="actor4"></actionStatus>
            </actionStatuses>
        </process>
        <process workflow="workflow1">
            <actionStatuses>
                <actionStatus action="action2" actor="actor5"></actionStatus>
                <actionStatus action="action4" actor="actor4"></actionStatus>
            </actionStatuses>
        </process>
    </processes>
</workflowManagementSystem>

Я хотел бы объявить некоторые ограничения, такие как:

1) Имя рабочего процесса должно быть уникальным.
2) Имя действия должно быть уникальным в рамках рабочего процесса, содержащего его.
3) Действие может быть повторено внутри процесса (больше actionStatus ссылается на то же действие).
4) actionStatus должен ссылаться только на действие, принадлежащее определенному рабочему процессу, созданному родительским процессом.
5) Актер, указанный в actionStatus, должен принадлежать к той же роли, которая указана в действии, к которому он относится.

Возможно ли это?

Ответы [ 2 ]

1 голос
/ 20 ноября 2011

Некоторые или ваши требования могут, другие не могут быть выполнены с использованием XML-схемы.

За одну заметку, прежде чем углубляться в детали, я также добавил дополнительный ключ / keyref (ссылочная целостность), который вы не просили, но я подумал, что это может быть полезно для будущих ссылок. UniqueWorkflowInWorkflows - это то, что вы просили; pkWorkflow / fkProcessToWorkflow, который я добавил. Между предложениями unique и key есть небольшие различия, но если вы примете key / keyref, вам не понадобится uniqueWorkflowInWorkflows.

Стоит упомянуть здесь, что в целом, если ваш дизайн допускает предложение xsd: unique, но не xsd: key; xsd: keyref может ссылаться на xsd: unique ...

Ответы:

1) См. UniqueWorkflowInWorkflows или pkWorkflow

2) См. УникальныйActionInWorkflow

3) Здесь нечего делать.

4) Невозможно выполнить в XML-схеме (я бы сказал, что это интуитивно понятно из-за ограничений в поддерживаемом синтаксисе XPath для селекторов).

5) Невозможно выполнить в схеме XML.

Ниже приведена схема XML, сгенерированная из вашего XML в соответствии со стилем разработки Russian Doll, единственное «глобальное» объявление - это объявление корневого элемента.

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="workflowManagementSystem">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="workflows">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="workflow">
                                <xsd:complexType>
                                        <xsd:sequence>
                                            <xsd:element name="actions">
                                                <xsd:complexType>
                                                    <xsd:sequence>
                                                <xsd:element maxOccurs="unbounded" name="action">
                                                    <xsd:complexType>
                                                        <xsd:attribute name="name" type="xsd:string" use="required"/>
                                                        <xsd:attribute name="role" type="xsd:string" use="required"/>
                                                    </xsd:complexType>
                                                </xsd:element>
                                            </xsd:sequence>
                                        </xsd:complexType>
                                    </xsd:element>
                                </xsd:sequence>
                                <xsd:attribute name="name" type="xsd:string" use="required"/>
                            </xsd:complexType>
                            <xsd:unique name="uniqueActionInWorkflow">
                                <xsd:selector xpath="actions/action"/>
                                <xsd:field xpath="@name"/>
                            </xsd:unique>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
                <xsd:unique name="uniqueWorkflowInWorkflows">
                    <xsd:selector xpath="workflow"/>
                    <xsd:field xpath="@name"/>
                </xsd:unique>
            </xsd:element>
            <xsd:element name="actors">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element maxOccurs="unbounded" name="actor">
                            <xsd:complexType>
                                <xsd:attribute name="name" type="xsd:string" use="required"/>
                                <xsd:attribute name="role" type="xsd:string" use="required"/>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="processes">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element maxOccurs="unbounded" name="process">
                            <xsd:complexType>
                                <xsd:sequence>
                                    <xsd:element name="actionStatuses">
                                        <xsd:complexType>
                                            <xsd:sequence>
                                                <xsd:element maxOccurs="unbounded" name="actionStatus">
                                                    <xsd:complexType>
                                                        <xsd:attribute name="action" type="xsd:string" use="required"/>
                                                        <xsd:attribute name="actor" type="xsd:string" use="required"/>
                                                    </xsd:complexType>
                                                </xsd:element>
                                            </xsd:sequence>
                                        </xsd:complexType>
                                    </xsd:element>
                                </xsd:sequence>
                                <xsd:attribute name="workflow" type="xsd:string" use="required"/>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:key name="pkWorkflow">
        <xsd:selector xpath="workflows/workflow"/>
        <xsd:field xpath="@name"/>
    </xsd:key>      
    <xsd:keyref name="fkProcessToWorkflow" refer="pkWorkflow">
        <xsd:selector xpath="processes/process"/>
        <xsd:field xpath="@workflow"/>
    </xsd:keyref>
</xsd:element>

Это визуализация источника XSD:

XSD Visualization

0 голосов
/ 22 ноября 2011

Я понял, что ограничения 4 и 5 не могут быть переведены в XML-схеме, но я решил оставшиеся вопросы, изменив структуру схемы и добавив некоторые дополнительные ограничения. Xml:

<workflowManagementSystem xmlns="http://www.example.org/wfInfo"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/wfInfo wfInfo.xsd ">
    <workflows>
        <workflow name="wf1">
            <actions>
                <action name="ac1" role="role1">
                </action>
                <action name="ac2" role="role2">
                </action>
                <action name="ac3" role="AddettoVendite"
                    automaticallyInstantiated="true">
                    <nextAction actionName="ac1"></nextAction>
                    <nextAction actionName="ac2"></nextAction>
                </action>
            </actions>
            <processes>
                <process startTime="2011-10-13T12:30:00Z">
                    <actionStatuses>
                        <actionStatus action="ac1" actor="actor3"
                            terminationTime="2011-10-13T12:30:00Z"></actionStatus>
                        <actionStatus action="ac3" actor="actor2"></actionStatus>
                    </actionStatuses>
                </process>
                <process startTime="2011-10-13T12:30:00Z">
                    <actionStatuses>
                        <actionStatus action="ac1" actor="actor1"></actionStatus>
                    </actionStatuses>
                </process>
            </processes>
        </workflow>
        <workflow name="wf2">
            <actions>
                <action name="ac2" role="role2"
                    automaticallyInstantiated="true">
                    <nextAction actionName="ac4"></nextAction>
                </action>
                <action name="ac4" role="role2"
                    automaticallyInstantiated="false">
                    <nextAction actionName="ac2"></nextAction>
                </action>
                <action name="ac5" role="role1"
                    automaticallyInstantiated="false">
                </action>
            </actions>
            <processes>

            </processes>
        </workflow>
    </workflows>
    <actors>
        <actor name="Actor1">
            <roles>
                <role name="Role3"></role>
                <role name="Role4"></role>
            </roles>
        </actor>
        <actor name="Actor2">
            <roles>
                <role name="Role1"></role>
            </roles>
        </actor>
        <actor name="Actor3">
            <roles>
                <role name="Role1"></role>
                <role name="Role2"></role>
            </roles>
        </actor>
    </actors>
</workflowManagementSystem>

И соответствующая схема:

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/wfInfo"
    xmlns:tns="http://www.example.org/wfInfo" elementFormDefault="qualified">

    <element name="workflowManagementSystem" type="tns:workflowManagementSystemType">

        <!-- CONSTRAINTS -->
        <key name="workflowKey">
            <selector xpath="tns:workflows/tns:workflow"></selector>
            <field xpath="@name"></field>
        </key>
        <keyref name="nextProcessRef" refer="tns:workflowKey">
            <selector
                xpath="tns:workflows/tns:workflow/tns:actions/tns:action/tns:nextProcess">
            </selector>
            <field xpath="@workflowName"></field>
        </keyref>

        <key name="actorKey">
            <selector xpath="tns:actors/tns:actor"></selector>
            <field xpath="@name"></field>
        </key>

        <keyref name="actorRef" refer="tns:actorKey">
            <selector
                xpath="tns:workflows/tns:workflow/tns:processes/tns:process/tns:actionStatuses/tns:actionStatus">
            </selector>
            <field xpath="@actor"></field>
        </keyref>

    </element>

    <!-- ROOT -->
    <complexType name="workflowManagementSystemType">
        <all>
            <!-- WORKFLOWS -->
            <element name="workflows" maxOccurs="1" minOccurs="1">
                <complexType>
                    <sequence>
                        <element name="workflow" type="tns:workflowType"
                            maxOccurs="unbounded" minOccurs="0">

                            <!-- CONSTRAINTS -->
                            <key name="actionKey">
                                <selector xpath="tns:actions/tns:action"></selector>
                                <field xpath="@name"></field>
                            </key>
                            <keyref name="nextActionRef" refer="tns:actionKey">
                                <selector xpath="tns:actions/tns:action/tns:nextAction"></selector>
                                <field xpath="@actionName"></field>
                            </keyref>
                            <keyref name="actionRef" refer="tns:actionKey">
                                <selector
                                    xpath="tns:processes/tns:process/tns:actionStatuses/tns:actionStatus"></selector>
                                <field xpath="@action"></field>
                            </keyref>
                        </element>
                    </sequence>
                </complexType>
            </element>

            <!-- ACTORS -->
            <element name="actors" maxOccurs="1" minOccurs="1">
                <complexType>
                    <sequence>
                        <element name="actor" type="tns:actorType" maxOccurs="unbounded"
                            minOccurs="0">

                        </element>
                    </sequence>
                </complexType>
            </element>
        </all>
    </complexType>

    <!-- WORKFLOW -->
    <complexType name="workflowType">
        <all>
            <!-- ACTIONS -->
            <element name="actions" maxOccurs="1" minOccurs="1">
                <complexType>
                    <sequence>
                        <element name="action" type="tns:actionType" maxOccurs="unbounded"
                            minOccurs="0"></element>
                    </sequence>
                </complexType>
            </element>
            <!-- PROCESSES -->
            <element name="processes" maxOccurs="1" minOccurs="1">
                <complexType>

                    <sequence>
                        <element name="process" type="tns:processType" maxOccurs="unbounded"
                            minOccurs="0">

                        </element>
                    </sequence>
                </complexType>
            </element>
        </all>
        <attribute name="name" type="string" use="required"></attribute>
    </complexType>

    <!-- PROCESS -->
    <complexType name="processType">
        <sequence>
            <!-- ACTIONSTATUSES -->
            <element name="actionStatuses" maxOccurs="1" minOccurs="1">
                <complexType>
                    <sequence>
                        <element name="actionStatus" type="tns:actionStatusType"
                            maxOccurs="unbounded" minOccurs="0">
                        </element>
                    </sequence>
                </complexType>

            </element>
        </sequence>
        <attribute name="startTime" type="dateTime" use="required"></attribute>
    </complexType>

    <!-- ACTION -->
    <complexType name="actionType">
        <choice>
            <element name="nextAction" maxOccurs="unbounded" minOccurs="0">
                <complexType>
                    <attribute name="actionName" type="string" use="required"></attribute>
                </complexType>
            </element>
            <element name="nextProcess" maxOccurs="1" minOccurs="1">
                <complexType>
                    <attribute name="workflowName" type="string" use="required"></attribute>
                </complexType>
            </element>
        </choice>
        <attribute name="name" type="string" use="required"></attribute>
        <attribute name="automaticallyInstantiated" type="boolean"
            use="required">
        </attribute>
        <attribute name="role" type="string" use="required"></attribute>
    </complexType>

    <!-- ACTIONSTATUS -->
    <complexType name="actionStatusType">
        <attribute name="action" type="string" use="required"></attribute>
        <attribute name="actor" type="string" use="optional"></attribute>
        <attribute name="terminationTime" type="dateTime" use="optional"></attribute>
    </complexType>

    <!-- ACTOR -->
    <complexType name="actorType">
        <sequence>
            <element name="roles" maxOccurs="1" minOccurs="1">
                <complexType>
                    <sequence>
                        <element name="role" type="tns:roleType" maxOccurs="unbounded"
                            minOccurs="1"></element>
                    </sequence>
                </complexType>
                <!-- CONSTRAINTS -->
                <unique name="actorRoleUnique">
                    <selector xpath="tns:role"></selector>
                    <field xpath="@name"></field>
                </unique>
            </element>
        </sequence>
        <attribute name="name" type="string" use="required"></attribute>
    </complexType>


    <complexType name="roleType">
        <attribute name="name" type="string" use="required"></attribute>
    </complexType>


</schema>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...