Sequence Tag

J

Jason Cavett

I'm curious about the sequence tag in XML. From my reading, the
sequence tag requires that the elements "must appear in the same order
as they are declared."

I personally don't care about the order of the elements in the XML
document (in fact, the elements could appear in any order within the
complex type) - they just have to be there. Can I use something
different other than sequence? I can't seem to find anything that
lets me do this.


Thanks
 
G

geoff

Use 'all', they all have to appear but in any order.

<xs:element name="letter">
<xs:complexType mixed="true">
<xs:all>
<xs:element name="greeting"/>
<xs:element name="body"/>
<xs:element name="closing"/>
</xs:all>
</xs:complexType>
</xs:element>

--g
 
J

Jason Cavett

Use 'all', they all have to appear but in any order.

<xs:element name="letter">
<xs:complexType mixed="true">
<xs:all>
<xs:element name="greeting"/>
<xs:element name="body"/>
<xs:element name="closing"/>
</xs:all>
</xs:complexType>
</xs:element>

--g

Okay. What if I want any of them to appear? For example, <xs:all>
means they all have to appear, but in any order, then what tag means
that they can appear in any order, but I don't REQUIRE any of them?
<xs:choice> seems to be right, but I just wanted to verify.

Thanks
 
M

Martin Honnen

Jason said:
Okay. What if I want any of them to appear? For example, <xs:all>
means they all have to appear, but in any order, then what tag means
that they can appear in any order, but I don't REQUIRE any of them?
<xs:choice> seems to be right, but I just wanted to verify.

With xs:choice you can specify
<xs:choice minOccurs="0">
so that the whole choice does not need to appear at all.
The same goes for xs:sequence.
 
J

Jason Cavett

With xs:choice you can specify
<xs:choice minOccurs="0">
so that the whole choice does not need to appear at all.
The same goes for xs:sequence.

Ah, okay. xs:sequence forces the order, though...that makes sense.
Thank you.

Is it possible to require that at least one of the elements appear
(and as long as at least one of the elements appear, the others don't
have to, but they can?).

Also, I'm more than willing to read about this rather than coming back
with all these questions. I just can't seem to find a good, concise
website that explains the types of things I'm wondering about. I've
been looking through the spec on w3, but it gets into far more detail
than I need.
 
M

Martin Honnen

Jason said:
Is it possible to require that at least one of the elements appear
(and as long as at least one of the elements appear, the others don't
have to, but they can?).

<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="el1" .../>
<xs:element name="el2" .../>
<xs:element name="el3" .../>
</xs:choice>
requires at least one of el1, el2, el3 and allows more of them.
 
J

Joseph Kesselman

Jason said:
Is it possible to require that at least one of the elements appear

Set minOccurs on the choice to 1.

(What you *can't* easily express in XML Schema would be a requirement
that only one instance of any of the choices occurs. To get that, you
wind up having to spell out most of the permutations explicitly. Most
common solution is to implement that constraint in the application code
or in a preprocessing stage such as a stylesheet; next most common
(because less portable) is to switch to an alternative schema language.
See past notes posted here for discussion.)
been looking through the spec on w3, but it gets into far more detail
than I need.

My standard pointer: There are a lot of tutorials and articles on
http://www.ibm.com/xml. (I'm sure there are other good websites too;
that just happens to be the one I hit most often other than the W3C's
own documents.)
 
J

Jason Cavett

<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="el1" .../>
<xs:element name="el2" .../>
<xs:element name="el3" .../>
</xs:choice>
requires at least one of el1, el2, el3 and allows more of them.

Hrm...I guess I misunderstood choice.

<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="el1" minOccurs="1" maxOccurs="1" />
<xs:element name="el2" />
<xs:element name="el3" />
</xs:choice>

In this case, I was thinking that the min/max on choice applied to
each element individually (AKA I could have 1 - unbounded el1, 1 to
unbounded el2, 1 to unbounded el3). However, by using min/max in el1,
el1 CAN ONLY HAVE 1 occurrence but can have no more than 1 occurrence.

So, I had already figured out the solution to my first problem...I
just didn't realize it. So...how would I limit different elements to
difference amounts of occurrences?


Thanks again for your help.
 
J

Joseph Kesselman

So, I had already figured out the solution to my first problem...I
just didn't realize it. So...how would I limit different elements to
difference amounts of occurrences?

Simplest solution: Go back to requiring that they occur in a specific
order, and set minOccurs/maxOccurs on each of them individually.

Otherwise... See my other response regarding trying to set quantity
limits without enforcing order. Basically, schema really isn't set up to
do that.
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top