Using loops in XSD?

C

Christian Rühl

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
 
P

Pavel Lepin

Christian Rühl said:
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>
 
C

Christian Rühl

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?


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

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! :)
 
U

usenet

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top