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

Newbie to XML - Need help on creating Schemas

Status
Not open for further replies.

anthony1709

Programmer
Nov 14, 2006
6
GB
Hi,

I'm just starting to learn XML, and I have a few questions. I know there are probably stupid and easy questions, but I need help. :p

Just wondering when writing a XML Schema, how do you write an attribute within a element when element is also a type as well. So here is my element:

<xs:element name="book">
<xs:complexType>
<xs:attribute ref="author"/>
</xs:complexType>
</xs:element>

I was just wondering how do I write the type for the element book, which is a "string"? Or I'm declaring the attribute at the wrong place, as the attribute author is an attribute of book. I hope this all makes sense? :p

Also got a few other quick questions, how do I make an attribute and element "required", and "implied" in schemas.
Also in DTD, what do the signs "*","+","?", mean?

Thanks very much, I hope some of this makes sense.
 
If you have an element like this... (note: I have included use="required" so that the attribute is mandatory. To make it optional change the code to use="optional"):
Code:
<xs:element name="book">
  <xs:complexType>
    <xs:attribute name="author" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>
...does not necessarily need to have a type:
Code:
<book author="J.K. Rowling" />

Personally, I don't implement major components of an element as attributes. So, depending on your scenario I would go for something like this:
Code:
<xs:element name="book">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="author" type="xs:string"/>
      <xs:element name="title" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

In DTDs,
? is used to indicate that a certain unit of an element can only appear at most once

+ is used to indicate that a certain unit of an element must appear at least once up to any limit

* is used to indicate that a certain unit of an element can appear as many times as necessary, or not at all.


It sounds like you need to do a bit of reading up. I would suggest and as a start. I've learnt quite a lot from a book called "XML for the World Wide Web" by Elizabeth Castro.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Maybe this.
[tt]<xs:element name="book">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="author"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:attribute name="author" type="xs:string" />
[/tt]
Example of valid node.
[tt] <book author="Beckett, Samuel">Waiting for Godot</book>
[/tt]
 
Hi,

Thanks for everyone's help, really appreciate it.

Yeah, Stretchwickster, you right, I need to some reading up, and I think I will look at that book you mentioned, so thanks for recommendation.

Tsuji, thanks for your help, that has done the trick, I'am able to do what I wanted to do now, so again thanks very much.

 
You're welcome. Thanks for posting some feedback regarding our suggestions. It always helps to know whether someone has found a response helpful.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top