xsd validation for a relation between two objects

40 views Asked by At

I have this xsd

 <xs:complexType name="ShapeLine">
    <xs:annotation>
        <xs:documentation>
            ShapeLine - the line between two shapes
        </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
        <xs:extension base="archimate:Line" >
            <xs:attribute name="category" type="xs:string" use="required"> 
            </xs:attribute> 
            <xs:attribute name="source" type="xs:IDREF" use="required" />
            <xs:attribute name="target" type="xs:IDREF" use="required" />
        </xs:extension>             
    </xs:complexContent>
</xs:complexType>

    
 <xs:complexType name="Shape">
    <xs:annotation>
        <xs:documentation>
            Shape object
        </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
        <xs:extension base="archimate:ViewNodeType" >                          
          <xs:attribute name="shapeId" type="xs:string" use="required">
                <xs:annotation>
                    <xs:documentation>
                        shapeId contains the id of the shape
                    </xs:documentation>
                </xs:annotation>
            </xs:attribute> 
              <xs:attribute name="name_internal" type="xs:string" use="required">
                <xs:annotation>
                    <xs:documentation>
                        name_internal contains the name_internal of the shape
                    </xs:documentation>
                </xs:annotation>
            </xs:attribute>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

and this is the xml that I need to validate:

 <views>
 <diagrams>
  <view identifier="id-7b98c8ec-243a-49fb-b6b0-baa68c95badb">
    <name xml:lang="EN">helloe</name>
    <node xsi:type="q1:Shape" identifier="id-1d599237- 
 76af-4fc7-8d9a-c4356c3b2137" x="10" y="30" w="1" h="1" name_internal="BD_shapes_av_Box" 
shapeId="5da9cedd0c0ba649f8cae72e" angle="0" isgroup="False" alignment="" textalign="0" size="72 72">            
    </node>
    <node xsi:type="q2:Shape" identifier="id-76efea7a-6cf3-4cf0-bbd4-e36597c0653b" x="454" y="54" w="1" h="1" name_internal="BD_shapes_av_Two sided arrow" shapeId="5dad549f0c0ba639c4a5a3ac" angle="0" isgroup="False" alignment="" textalign="0" size="72 30,476">         
    </node>
    <connection xsi:type="q3:ShapeLine" identifier="id-4ab26cbf-509b-4657-b066-1a676a2773eb" source="id-1d599237-76af-4fc7-8d9a-c4356c3b2137" target="id-76efea7a-6cf3-4cf0-bbd4-e36597c0653b" category="line_dottednoarrows">         
      <sourceAttachment x="82" y="66" />
      <targetAttachment x="454" y="69" />
    </connection>
  </view>
</diagrams>

I need to create an xsd validation in order that a ShapeLine to be allowed only between objects of type Shape. Can be this achievable in xsd? I'm quite new to xsd-s, so any help is appreciated.

1

There are 1 answers

0
martijn On

I'm not really sure that I understood the question correct. I think I'm missing required information. You are asking to create an xml schema definition that validates the xml to make sure that elements of the type shape can have child element of the type shapeline. But looking at the example xml the element of the type shapeline is a sibling of the elements of the type shape. And the q1:Shape en q2:Shape gives me the idea that there are 2 Shape types with a different namespace or the q1 and q2 namespace prefix is pointing to the same actual namespace.

Types must be assigned to elements or attributes and those elements and attributes are creating the layout of the message. you cannot create an xsd using only types to describe an xml message. you need to assign the types to elements and or attributes. The order and the structure you create with those elements and attributes will determine how the xml message will look like.

the below xml example does have both options in it. First the sibling child and then the sibling sibling

<?xml version="1.0" encoding="UTF-8"?>
<ns:root xmlns:ns="http://tempuri.org/example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns:Sibling1 attr1="text" attr2="text">
        <ns:Child1 xsi:type="ns:ShapeLine">
            <ns:Child2>text</ns:Child2>
            <ns:Child3>text</ns:Child3>
        </ns:Child1>
    </ns:Sibling1>
    <ns:Sibling2 attr1="text" attr2="text" xsi:type="ns:Shape"/>
    <ns:Sibling3 xsi:type="ns:ShapeLine">
        <ns:Child2>text</ns:Child2>
        <ns:Child3>text</ns:Child3>
    </ns:Sibling3>
</ns:root>

This example xml is generated from the below xsd.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://tempuri.org/example" targetNamespace="http://tempuri.org/example" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Sibling1">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:extension base="ns:Shape">
                                <xs:sequence>
                                    <xs:element name="Child1" type="ns:ShapeLine"/>
                                </xs:sequence>
                            </xs:extension>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Sibling2" type="ns:Shape"/>
                <xs:element name="Sibling3" type="ns:ShapeLine"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="Shape">
        <xs:attribute name="attr1"/>
        <xs:attribute name="attr2"/>
    </xs:complexType>
    <xs:complexType name="ShapeLine">
        <xs:sequence>
            <xs:element name="Child2"/>
            <xs:element name="Child3"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

This is a very simplified version of what you want to achieve, but hopefully it will help you to get started.