XML Schema Problem

Wee

Wee

Associate
Joined
25 Apr 2006
Posts
130
Location
Scotland
I'm creating an XML document covering one of the groups in the Champoins League, and using a Schema to make sure it's valid and what not.

It's almost done, but I've hit a snag. I have a complex element, called goals, that contains another element, called scorer. It looks like this:

Code:
<goals>
   <scrorer number="1" time="62mins" team="Manchester United">Wayne Rooney</scrorer>
</goals>

My problem is that a game may finish 0 - 0, resulting in no goals, meaning that the scorer element won't be needed. Is there anyway in the Schema (or any other way) to take this into consideration?

Cheers.
 

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
<goals/>

I'm guessing you won't need any scorers, but the closed element will represent zero for you nicely

for your schema, you might want to use a single <scorer/> element rather than omitting it altogether, depending on how you're working it
 

Wee

Wee

Associate
OP
Joined
25 Apr 2006
Posts
130
Location
Scotland
This is my first go at XML, so bare with me as I'm not totally sure what you mean. :)

Does the <goal/> mean it will become an empty element? And if so, where does the scorer element fit into all of this?
 

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
yeah, I was just thinking that for your schema to validate, you'll probably have to use:

Code:
<goals>
  <scorer/>
</goals>

rather than just <goals/> - and yes, it becomes an empty element
 

Wee

Wee

Associate
OP
Joined
25 Apr 2006
Posts
130
Location
Scotland
Ah, got'cha. :)

So all the data for the element <scorer>, now that it is becoming an empty element, will need to be an attribute? Like so:

Code:
<goal>
<scorer number="1" time="62mins" player="Wayne Rooney" team="Manchester United"/>
</goal>
 

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
if it's a 0-0 game, there won't be any scorers though will there? an empty element (<scorer/>) and a full element (<scorer>Wayne Rooney</scorer>) are interchangeable in a valid schema. there's no need to add player attribute - only use the empty element if there's no scorers, otherwise, as you were doing in the OP
 
Associate
Joined
12 Mar 2005
Posts
2,021
Location
Scotland
BUMP (mainy aimed at Sic):

EDIT FOR UPDATE:

I can now validate xs:string with the us of nillable="true" but this wont work for integer. google isnt helping much.

an empty element (<scorer/>) and a full element (<scorer>Wayne Rooney</scorer>) are interchangeable in a valid schema.

I am having very similar problems on validating empty elements (in my case empty elements with no attributes which i think is what causing the prob)


xml
Code:
 <REDCARDS>
    <NAME/>
    <TIME/>
 </REDCARDS>

So an complex element REDCARDS containing elements that may or may not be empty with no attributes.

how could i validate this in my Schema?

(what i have so far but not validating it)

xsd
Code:
 <xs:element name="REDCARDS" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
                 <xs:sequence>
                     <xs:element name="NAME" type="xs:string" minOccurs="0" nillable="yes"/>
                     <xs:element name="TIME" type="xs:integer" minOccurs="0"/>
                  </xs:sequence>
            </xs:complexType>
</xs:element>
 
Last edited:
Back
Top Bottom