Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Schema / Confusion

Status
Not open for further replies.

philevans82

Programmer
Jul 16, 2007
3
GB
I need some help! The problem I have is that a "menu" can have a nested "menu". That works okay. But the problem is that after a nested menu, there could be a menuitem and the current XSD i have done below doesnt allow for that. Please help!

I have the following XSD

Code:
<xs:schema xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]

<xs:element name="frame" type="frameTYPE"/>

<xs:complexType name="frameTYPE">
    <xs:sequence>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="menubar" minOccurs="0" maxOccurs="1" type="menubarTYPE"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="menubarTYPE">
    <xs:sequence>
        <xs:element name="menu" minOccurs="0" maxOccurs="unbounded" type="menuTYPE"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="menuTYPE">
    <xs:sequence>
        <xs:element name="title" type="xs:string" maxOccurs="1"/>
        <xs:element name="menuitem" type="menuitemTYPE" minOccurs="0" maxOccurs="unbounded"/> 
        <xs:element name="menu" type="menuTYPE" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="menuitemTYPE">
    <xs:all>
       <xs:element name="icon" minOccurs="0" maxOccurs="1" type="xs:string"/>
       <xs:element name="title" type="xs:string"/>
       <xs:element name="action" minOccurs="0" maxOccurs="1" type="xs:string"/>
    </xs:all>
</xs:complexType>


</xs:schema>

and the following XML file

Code:
<frame>
    <title>Test</title>
    <menubar>
        <menu>
            <title>File</title>
            <menuitem>
                <icon>WordIcon2.jpg</icon>
                <title>New...</title>
                <action>newRecord()</action>
            </menuitem>
        </menu>
        <menu>
            <title>File</title>
            <menuitem>
                <icon>WordIcon2.jpg</icon>
                <title>New...</title>
                <action>newRecord()</action>
            </menuitem>
        </menu>
        <menu>
            <title>File</title>
            <menuitem>
                <icon>WordIcon2.jpg</icon>
                <title>New...</title>
                <action>newRecord()</action>
            </menuitem>
            <menu>
                <title>File</title>
                <menuitem>
                    <icon>WordIcon2.jpg</icon>
                    <title>New...</title>
                    <action>newRecord()</action>
                </menuitem>
                <menuitem>
                    <icon>WordIcon2.jpg</icon>
                    <title>New...</title>
                    <action>newRecord()</action>
                </menuitem>
            </menu>
            <menuitem>
                <icon>WordIcon2.jpg</icon>
                <title>New...</title>
                <action>newRecord()</action>
            </menuitem>
        </menu>
    </menubar>
</frame>
 
[1] You've to enlist all the <menuitem>'s before starting to list <menu>'s. Like this.
[tt]
<frame>
<title>Test</title>
<menubar>
<menu>
<title>File</title>
<menuitem>
<icon>WordIcon2.jpg</icon>
<title>New...</title>
<action>newRecord()</action>
</menuitem>
</menu>
<menu>
<title>File</title>
<menuitem>
<icon>WordIcon2.jpg</icon>
<title>New...</title>
<action>newRecord()</action>
</menuitem>
</menu>
<menu>
<title>File</title>
<menuitem>
<icon>WordIcon2.jpg</icon>
<title>New...</title>
<action>newRecord()</action>
</menuitem>
[blue]
<!-- moved up to here before menu's -->
<menuitem>
<icon>WordIcon2.jpg</icon>
<title>New...</title>
<action>newRecord()</action>
</menuitem>
[/blue]
<menu>
<title>File</title>
<menuitem>
<icon>WordIcon2.jpg</icon>
<title>New...</title>
<action>newRecord()</action>
</menuitem>
<menuitem>
<icon>WordIcon2.jpg</icon>
<title>New...</title>
<action>newRecord()</action>
</menuitem>
</menu>
[red]<!-- moved up[/red]
<menuitem>
<icon>WordIcon2.jpg</icon>
<title>New...</title>
<action>newRecord()</action>
</menuitem>
[red]-->[/red]
</menu>
</menubar>
</frame>
[/tt]
[2] The reason being the menuType's structure is defined with <xs:sequence> where "meuitem" preceeds "menu".
[tt]
<xs:complexType name="menuTYPE">
<xs:sequence>
<xs:element name="title" type="xs:string" maxOccurs="1"/>
[blue]<xs:element name="menuitem" type="menuitemTYPE" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="menu" type="menuTYPE" minOccurs="0" maxOccurs="unbounded"/>[/blue]
</xs:sequence>
</xs:complexType>
[/tt]
 
Sorry,

I should have added that the XML I provided is incorrect. the elements in menuTYPE can appear any number of times and in any order.

I've done a bit of research on the net and it looks like I am unable to use xsd to allow this because sequence has to be in order an all can only have maxOccurs of 1.

I'm probably going to consider using RELAX NG. Do you think this is the best option?

As a side note, is it possible to have a "random" order of elements using DTD?
 
[1] If you want any order, use xs:all instead.

[2]
>I've done a bit of research on the net and it looks like I am unable to use xsd to allow this because sequence has to be in order an[sic] [red]all can only have maxOccurs of 1[/red].
Really?! where do you get that kind of statement?
 

Code:
All Indicator

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:

<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>

Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1 (the <minOccurs> and <maxOccurs> are described later).
 
Okay.
>>I've done a bit of research on the net and it looks like I am unable to use xsd to allow this because sequence has to be in order [red]an[sic] all[/red] can only have maxOccurs of 1.
I though you meant "[tt]an[highlight]d[/highlight] all[/tt]". And you actually meant "[tt]an [xs:]all [element][/tt]". My bad!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top