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

Is this simple XML exercise correct?

Status
Not open for further replies.

Jazztronik

Programmer
Jul 14, 2005
2
ES
Hi, I'm new to XML, and I have to do an exercise which simulates values related to the state of a traffic light.

This is the DTD I wrote:

<?xml version"1.0" encoding="utf-8"?>
<!DOCTYPE trafficLights[
<!ELEMENT trafficLights (trafficLight+)>
<!ELEMENT trafficLight (#PCDATA)>
<!ATTLIST trafficLight State (red | yellow | green) "green">
]>


And here's an example I made which matches the DTD:

<trafficLights>
<trafficLight State="red">1</trafficLight>
<trafficLight State="yellow">2</trafficLight>
<trafficLight State="red">3</trafficLight>
<trafficLight State="green">4</trafficLight>
<trafficLight State="green">5</trafficLight>
</trafficLights>


Well, I guess it's written correctly, but what I actually want to know is if there's another better way to perform this exercise. For instance, is it better to identify each traffic light with numbers between tags like I made? or by using an attribute?

Any help would be really appreciated!

 
That old elements/attributes chestnut. It doesn't really matter, they are essentially the same thing. The only difference being that elements are extensible.

I'm not entirely sure what you are trying to represent with this XML, but do you really need the numbering? Isn't the position in the list enough? ie, what is wrong with:
Code:
<trafficLights>
  <trafficLight State="red"/>
  <trafficLight State="yellow"/>
  <trafficLight State="red"/>
  <trafficLight State="green"/>
  <trafficLight State="green"/>
</trafficLights>
Also, I would use an XML Schema rather than a DTD.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Thanks JontyMC!

With those numbers I wanted to represent an ID unique to each element. However I realized it would have been better by using an attribute which type is ID.

I know the XML Schemas are better, but I'm just learning it from the beginning and still haven't seen XML-Schema. The exercise asked me for a DTD.

Greetings! :)
 
You don't need to use an ID because they are already uniquely identified by their respective positions in the node set.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top