Hamal000
Hamal000

Reputation: 516

Integrity constraint in the XML schema

I have an XML like this (a workflow represents a sort of a class that is instantiated by a process).

<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>

I'd like to declare some constraints like:

1) The name of the workflow has to be unique.
2) The name of the action has to be unique within the scope of the workflow containing it.
3) An action can be repeated within the process (more actionStatus referring to the same action).
4) An actionStatus should refer only to the action belonging to the particular workflow instantiated by the father process.
5) The actor indicated in actionStatus must belong to the same role as specified in the action to which it refers.

Is it possible?

Upvotes: 1

Views: 1062

Answers (2)

Hamal000
Hamal000

Reputation: 516

I have recognized that constraints 4 and 5 can't be translated in xml schema, but I've resolved the remaining questions editing the schema structure and adding some extra constraints. 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>

And the corresponding schema is:

<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>

Upvotes: 0

Petru Gardea
Petru Gardea

Reputation: 21658

Some or your requirements can, others cannot be done using XML Schema.

One note before going into details, I've also added an additional key/keyref (referential integrity), that you did not ask for, but I thought it might be good for future references. The uniqueWorkflowInWorkflows is what you've asked for; the pkWorkflow/fkProcessToWorkflow I've added. There are subtle differences between unique and key clauses, but if you accept the key/keyref, you don't need uniqueWorkflowInWorkflows.

It is worth mentioning here that in general, if your design allows for an xsd:unique clause, but not an xsd:key; an xsd:keyref can reference an xsd:unique...

The answers are:

1) See uniqueWorkflowInWorkflows or pkWorkflow

2) See uniqueActionInWorkflow

3) Nothing to do here.

4) Cannot be done in XML Schema (intuitively I would say due to limitations in the supported XPath syntax for the selectors).

5) Cannot be done in XML Schema.

Below is an XML Schema generated from your XML, following a Russian Doll authoring style, that is the only "global" declaration is that of the root element.

<?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>

This is a visualization of the XSD source:

XSD Visualization

Upvotes: 1

Related Questions