Using loops in XSD?

Discussion in 'XML' started by Christian Rühl, Nov 19, 2007.

  1. hi @all!

    i have a little problem with xsd. is it possible to define a loop in a
    schema?

    the xml files i want to validate look like this:

    - product
    -- component
    --- sub-component
    --- sub-component
    ---- sub-component
    -- component
    --- sub-component
    ---- sub-component

    each (sub-)component can have one or more own sub-components and so
    on... there is a static solution needed because the xml file is
    usually written by hand. writing it by using to many references makes
    it more likely to make a mistake and more difficult to locate an
    error.

    i really hope you can help me with that. i didn't find a real hint
    yet. :(

    Mfg,
    Christian Rühl
     
    Christian Rühl, Nov 19, 2007
    #1
    1. Advertising

  2. Christian Rühl

    Pavel Lepin Guest

    Christian Rühl <> wrote in
    <>:
    > the xml files i want to validate look like this:
    >
    > - product
    > -- component
    > --- sub-component
    > --- sub-component
    > ---- sub-component
    > -- component
    > --- sub-component
    > ---- sub-component


    And why, pray tell me, didn't you post an actual XML
    document that we would be able to stuff into our parsers?
    To make life more interesting for us?

    > each (sub-)component can have one or more own
    > sub-components and so on...


    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="sub">
    <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element ref="sub"/>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Nov 19, 2007
    #2
    1. Advertising

  3. On 19 Nov., 14:57, Pavel Lepin <> wrote:
    > Christian Rühl <> wrote in
    > <>:
    >
    > > the xml files i want to validate look like this:

    >
    > > - product
    > > -- component
    > > --- sub-component
    > > --- sub-component
    > > ---- sub-component
    > > -- component
    > > --- sub-component
    > > ---- sub-component

    >
    > And why, pray tell me, didn't you post an actual XML
    > document that we would be able to stuff into our parsers?
    > To make life more interesting for us?
    >
    > > each (sub-)component can have one or more own
    > > sub-components and so on...

    >
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    > <xs:element name="sub">
    > <xs:complexType>
    > <xs:choice minOccurs="0" maxOccurs="unbounded">
    > <xs:element ref="sub"/>
    > </xs:choice>
    > </xs:complexType>
    > </xs:element>
    > </xs:schema>
    >
    > --
    > ...also, I submit that we all must honourably commit seppuku
    > right now rather than serve the Dark Side by producing the
    > HTML 5 spec.


    wow, that was easy! i should have known that... in fact i've been
    working with that a little earlier in another project... maybe i just
    don't get enough sleep! :D
    thanks a lot anyway! you helped me before! :)
     
    Christian Rühl, Nov 19, 2007
    #3
  4. Christian Rühl

    Guest

    On 19 Nov, 14:41, "Christian Rühl" <> wrote:
    > On 19 Nov., 14:57, Pavel Lepin <> wrote:
    > > Christian Rühl <> wrote in
    > > <>:

    >
    > > > the xml files i want to validate look like this:
    > > > -- component
    > > > --- sub-component
    > > > --- sub-component
    > > > ---- sub-component
    > > > -- component
    > > > --- sub-component
    > > > ---- sub-component

    >
    > > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    > > <xs:element name="sub">
    > > <xs:complexType>
    > > <xs:choice minOccurs="0" maxOccurs="unbounded">
    > > <xs:element ref="sub"/>
    > > </xs:choice>
    > > </xs:complexType>
    > > </xs:element>
    > > </xs:schema>


    Another option might be:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="sub">
    <xs:complexType>
    <xs:sequence>
    <!-- You might want some extra stuff here. -->
    <xs:element ref="sub" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    Or (tweak of Pavel's):

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="sub">
    <xs:complexType>
    <xs:choice>
    <xs:element ref="subdetails" type="subdetails"/>
    <xs:element ref="sub" minOccurs="0" maxOccurs="unbounded"/>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    The difference between the two depends on whether a sub-component with
    sub-components _only_ have sub-components, or has other data plus sub-
    components. (Wow - that's a lot of sub-components!)

    HTH,

    Pete Cordell
    Codalogic
    Visit http://www.codalogic.com/lmx/
    for XML Schema to C++ data binding
     
    , Nov 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,137
    Markus
    Nov 22, 2005
  2. Rick Razzano

    XSD document for XSD defintion

    Rick Razzano, Sep 26, 2003, in forum: XML
    Replies:
    1
    Views:
    505
    C. M. Sperberg-McQueen
    Sep 26, 2003
  3. Replies:
    1
    Views:
    878
    Martin Honnen
    Jan 14, 2004
  4. Peter Aberline

    xsd:any as a child of xsd:all

    Peter Aberline, Apr 5, 2004, in forum: XML
    Replies:
    0
    Views:
    803
    Peter Aberline
    Apr 5, 2004
  5. Me
    Replies:
    2
    Views:
    253
Loading...

Share This Page