Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XML Schema Assistance

Options
  • 08-11-2016 8:42pm
    #1
    Registered Users Posts: 8,197 ✭✭✭


    I've got to create an XML schema for a task I'm currently working on. It has a number of nested elements and I'm unsure as how to create this. From reading online it seems straightforward, but I'm not able to get it working.

    The skelton of the output should be as follows (there should be up to 6 instances of the contents of <Subtitle1></Subtitle1>).
    <Title>
        <Subtitle1>
            <Subtitle2>
                <ItemTypeA1>ItemA1<ItemTypeA1>
                <ItemTypeA2>ItemA2<ItemTypeA2>
                <ItemTypeA3>ItemA3<ItemTypeA3>
                <ItemTypeA4>ItemA4<ItemTypeA4>
            </Subtitle2>
            <Subtitle3>
                <ItemTypeB1>ItemB1<ItemTypeB1>
                <ItemTypeB2>ItemB2<ItemTypeB2>
                <ItemTypeB3>ItemB3<ItemTypeB3>
            </Subtitle3>
    
        <!--                              --!>
        <!-- Repeat <Subtitle3> 6 times   --!>
        <!--                              --!>
    
            <Subtitle3>
                <ItemTypeB1>ItemB1<ItemTypeB1>
                <ItemTypeB2>ItemB2<ItemTypeB2>
                <ItemTypeB3>ItemB3<ItemTypeB3>
            </Subtitle3>
        </Subtitle1>
    
        <!--                              --!>
        <!-- Repeat <Subtitle1> 4 times   --!>
        <!--                              --!>
    
        <Subtitle1>
            <Subtitle2>
                <ItemTypeA1>ItemA1<ItemTypeA1>
                <ItemTypeA2>ItemA2<ItemTypeA2>
                <ItemTypeA3>ItemA3<ItemTypeA3>
                <ItemTypeA4>ItemA4<ItemTypeA4>
            </Subtitle2>
            <Subtitle3>
                <ItemTypeB1>ItemB1<ItemTypeB1>
                <ItemTypeB2>ItemB2<ItemTypeB2>
                <ItemTypeB3>ItemB3<ItemTypeB3>
            </Subtitle3>
    
        <!--                              --!>
        <!-- Repeat <Subtitle3> 6 times   --!>
        <!--                              --!>
    
            <Subtitle3>
                <ItemTypeB1>ItemB1<ItemTypeB1>
                <ItemTypeB2>ItemB2<ItemTypeB2>
                <ItemTypeB3>ItemB3<ItemTypeB3>
            </Subtitle3>
        </Subtitle1>
    </Title>
    
    I've been trying to figure out how to do this - mainly looking at the following: http://stackoverflow.com/questions/11148609/how-do-you-nest-complextype-elements-in-an-xsd

    And I've come up with the following. Is this the correct approach? I'm not sure how to follow the approach in the original post in the link, but I've attempted something similar to the second reply.
    </xs:schema>
        <xs:complexType name="Title">
            <xs:sequence>
                <xs:element name="Subtitle1" type="Subtitle1" maxOccurs="6" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="Subtitle1">
            <xs:sequence>
                <xs:element name="Subtitle2" type="Subtitle2" maxOccurs="1" minOccurs="1"/>
                <xs:element name="Subtitle3" type="Subtitle3" maxOccurs="8" minOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="Subtitle2">
            <xs:sequence>
                <xs:element name="ItemA1" type="ItemTypeA1" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemA2" type="ItemTypeA2" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemA3" type="ItemTypeA3" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemA4" type="ItemTypeA4" maxOccurs="1" minOccurs="1"/>
           </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="Subtitle3">
            <xs:sequence>
                <xs:element name="ItemB1" type="ItemTypeB1" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemB2" type="ItemTypeB2" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemB3" type="ItemTypeB3" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemB4" type="ItemTypeB4" maxOccurs="1" minOccurs="1"/>
           </xs:sequence>
        </xs:complexType>
    
    </xs:schema>
    
    However, I'm not sure how to ensure it will handle if there are less than the maximum number of items relating to Subtitle1.

    Am I doing this correctly? Any help greatly appreciated.


    Thanks.



    EDIT: I've got my schema producing an output. Should the Schema fragement displayed at the top of the Java output file replicate the structure I am trying to create? The fragement I've got is has everything on a single layer and no indication of a nested structure.

    Any help would be greatly appreciated.



    (PS - apologies for the generic naming conventions, but as this is work related I'd rather not list actual names).


Comments

  • Registered Users Posts: 8,197 ✭✭✭funkey_monkey


    Hi guys,

    Still looking some help with this. I'm back working on this, but I'm having some trouble getting it to produce the XML in the correct format.

    Currently, I'm getting:


    I've modified my schema to the following:
    </xs:schema>
        <xs:complexType name="Title">
            <xs:sequence>
                <xs:element name="Title1" type="First_Subtitle" maxOccurs="1" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="First_Subtitle">
            <xs:sequence>
                <xs:element name="Subtitle1" type="SubtitleCombo" maxOccurs="4" minOccurs="1"/>
                <xs:element name="Checksum" type="CHKSM" maxOccurs="1" minOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="Subtitle Combo">
            <xs:sequence>
                <xs:element name="Subtitle2" type="Second_Subtitle" maxOccurs="1" minOccurs="1"/>
                <xs:element name="Subtitle3" type="Third_Subtitle" maxOccurs="6" minOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="Second_Subtitle">
            <xs:sequence>
                <xs:element name="ItemA1" type="ItemTypeA1" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemA2" type="ItemTypeA2" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemA3" type="ItemTypeA3" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemA4" type="ItemTypeA4" maxOccurs="1" minOccurs="1"/>
           </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="Third_Subtitle">
            <xs:sequence>
                <xs:element name="ItemB1" type="ItemTypeB1" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemB2" type="ItemTypeB2" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemB3" type="ItemTypeB3" maxOccurs="1" minOccurs="1"/>
                <xs:element name="ItemB4" type="ItemTypeB4" maxOccurs="1" minOccurs="1"/>
           </xs:sequence>
        </xs:complexType>
    
    </xs:schema>
    


    Currently I'm getting:
    <Title>
        <Subtitle1>
            <Subtitle2>
            ...
            </Subtitle2>
            <Subtitle3>
            ...
            </Subtitle3>
        </Subtitle1>
    </Title>
    <Title>
        <Subtitle1>
            <Subtitle2>
            ...
            </Subtitle2>
            <Subtitle3>
            ...
            </Subtitle3>
        </Subtitle1>
    </Title>
    

    What is causing the <title></title> annotation to be repeatedly produced instead of once?

    Also, why am I getting the following message:
    
    cvc-complex-type.2.4.d: Invalid content was found starting with element 'Title'. No child element is expected at this point.

    Any suggestions?


Advertisement