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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Schema definition

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
0
0
GB
How could I specify in an XML Schema (*.xsd) that an element should have an attribute

e.g. how to specify <person> needs to take an id and name attribute e.g.

<person id=&quot;1234567&quot; name=&quot;how to do it?&quot; />
 
Define a complex (or simple) type for the person element and specify there the attributes required:

<xs:complexType name=&quot;ctPerson&quot;>
<xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot;/>
<xs:attribute name=&quot;id&quot; type=&quot;xs:integer&quot;/>
</xs:complexType>

Then, define the element Person having the base type the above typedef:

<xs:element name=&quot;Person&quot; type=&quot;ctPerson&quot;/>

This should do it... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Yes, such a definition would support
Code:
<person id=&quot;1234567&quot; name=&quot;how to do it?&quot; />

Unfortunately, what I really need the schema to allow is

Code:
<person id=&quot;1234567&quot; name=&quot;how to do it?&quot; >Something do do with Person</person>

With the schema definied using a complext type, I get error,Unexpected element contents- person is defined as EMPTY

The following works, but I don't want an extra 'level' of tag:
Code:
<xs:complexType name=&quot;ctPerson&quot;>
  <xs:sequence>
     <xs:element name=&quot;ctPerson&quot; type=&quot;xs:string&quot; />
  </xs:sequence>
  <xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot;/>
  <xs:attribute name=&quot;id&quot; type=&quot;xs:integer&quot;/>
</xs:complexType>
 
Indeed, I'm sorry for the ommision. The definition I put there has empty content.

You have to additionaly specify <complexContent> or <simpleContent> into the complexType definition. You use <complexContent> if the element you describe is going to have child elements, or <simpleContent> if it is going to contain only simple data - like you want to.

I guess that the definition below should do it:

<xs:complexType name=&quot;ctPerson&quot;>
<xs:simpleContent>
<xs:extension base=&quot;xs:string&quot;>
<xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot;/>
<xs:attribute name=&quot;id&quot; type=&quot;xs:integer&quot;/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
The Professional XML Schemas book from WROX publishers is an excellent reference for XML Schema. It's a little rough to get through at first, but great at answering these questions when they come up.

Uura
~~~~
&quot;Common sense tells you that the world is flat.&quot;
 
I vote for Wrox that too... They publish excellent books. You should go for Proffesional XML too.

But, besides the using the &quot;mixed&quot; content solution that flumpy suggested at some point, having both simple and complex content in an element is simply not possible with schema language. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top