tags:

views:

42

answers:

4

Okay this may sound silly, but I am not very familiar with XML.

What I try to do is this:

I have two entities: Company and Employee. Both entities only have a name property.

How would an XML document look like, when I had two companies with each having two employees? Could I simply do something like this? :

<?xml version="1.0"?>
  <company>
    <name>Apple</name>
    <employee>Steve</employee>
    <employee>Bill</employee>
  </company>
  <company>
    <name>Banana</name>
    <employee>John</employee>
    <employee>Luke</employee>
  </company>

Is that good syntax? Or must I do it differently then that? Is there missing anything important?

Edit For comparison I copied an valid XML example from USGS (an earthquake watch institute that publishes earthquake data via xml):

<?xml version="1.0"?>
<feed xml:base="http://earthquake.usgs.gov/" xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss"&gt;
  <updated>2010-01-26T09:36:26Z</updated>
  <title>USGS M2.5+ Earthquakes</title>
  <subtitle>Real-time, worldwide earthquake list for the past day</subtitle>
  <link rel="self" href="/earthquakes/catalogs/1day-M2.5.xml"/>
  <link href="http://earthquake.usgs.gov/earthquakes/"/&gt;
  <author><name>U.S. Geological Survey</name></author>
  <id>http://earthquake.usgs.gov/&lt;/id&gt;
  <icon>/favicon.ico</icon>
  <entry><id>urn:earthquake-usgs-gov:us:2010ryav</id><title>M 5.2, southern Sumatra, Indonesia</title><updated>2010-01-26T06:53:25Z</updated><link rel="alternate" type="text/html" href="/earthquakes/recenteqsww/Quakes/us2010ryav.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/0_100.jpg" alt="0.343&#176;S 99.024&#176;E" align="left" hspace="20" /><p>Tuesday, January 26, 2010 06:53:25 UTC<br>Tuesday, January 26, 2010 01:53:25 PM at epicenter</p><p><strong>Depth</strong>: 58.50 km (36.35 mi)</p>]]></summary><georss:point>-0.3427 99.0242</georss:point><georss:elev>-58500</georss:elev><category label="Age" term="Past day"/></entry>


  <entry><id>urn:earthquake-usgs-gov:pr:p1002601</id><title>M 2.7, Puerto Rico region</title><updated>2010-01-26T04:14:07Z</updated><link rel="alternate" type="text/html" href="/earthquakes/recenteqsww/Quakes/prp1002601.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.102&#176;N 66.415&#176;W" align="left" hspace="20" /><p>Tuesday, January 26, 2010 04:14:07 UTC<br>Tuesday, January 26, 2010 12:14:07 AM at epicenter</p><p><strong>Depth</strong>: 13.80 km (8.57 mi)</p>]]></summary><georss:point>19.1017 -66.4150</georss:point><georss:elev>-13800</georss:elev><category label="Age" term="Past day"/></entry>


  <entry><id>urn:earthquake-usgs-gov:hv:00036408</id><title>M 3.0, Island of Hawaii, Hawaii</title><updated>2010-01-26T00:37:14Z</updated><link rel="alternate" type="text/html" href="/earthquakes/recenteqsww/Quakes/hv00036408.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-155.jpg" alt="19.206&#176;N 155.517&#176;W" align="left" hspace="20" /><p>Tuesday, January 26, 2010 00:37:14 UTC<br>Monday, January 25, 2010 02:37:14 PM at epicenter</p><p><strong>Depth</strong>: 37.70 km (23.43 mi)</p>]]></summary><georss:point>19.2063 -155.5173</georss:point><georss:elev>-37700</georss:elev><category label="Age" term="Past day"/></entry>

</feed>

They use no root element like <entries>! And Apple's seismicXML example works just fine with this thing. Strange...

+2  A: 

You're missing a root element, such as:

<?xml version="1.0"?>
<companies>
    <company>...</company>
    <company>...</company>
</companies>

Otherwise I think this structure is fine for representing a one-to-many relationship of companies' employees.


In response to the USGS feed you posted, it does have a root element, feed.

Ben James
updated my question... are you sure about that root element thing? Apple's XML doesn't have it and works... confusing
openfrog
There is a root element, it is `feed`
Ben James
I see... so the root element has nothing to do with "there are many companies, so they need a root"...because then this would also apply to employee inside companies, and so on...
openfrog
+1  A: 

You might also consider combing the previous example while having name as an attribute, like so:

<?xml version="1.0"?>
<companies>
    <company name="Apple">
        <employee>Steve</employee>
        <employee>Bill</employee>
    </company>
    <company name="Banana">
        <employee>John</employee>
        <employee>Luke</employee>
    </company>
</companies>
PatrickJ
A: 

You should NOT rely on naming. You should give each employee an id (the same for the company). Because otherwise you cannot have two employees with the identical name.

Example:

<data>
    <company id="0">
      <name>Apple</name>
      <employee>0</employee>
      <employee>1</employee>
    </company>
    <company id="1">
      <name>Banana</name>
      <employee>2</employee>
      <employee>3</employee>
    </company>
    <employee id="0" name="Steve"/>
    <employee id="1" name="Bill"/>
    <employee id="2" name="John"/>
    <employee id="3" name="Luke"/>
  </data>
Karussell
+1  A: 

XML wasn't really made for data modeling. It was made for data serialization. It's better to model using ER diagrams and think of the XML representation only after you've decided on the data model.

Generally, when your relationships form a tree, you can model them with subelements. When they don't, you may prefer to introduce references of some kind. For instance, in this case, including employee elements into company elements will suffice as long as every employee is associated with exactly one company. But if the same employee has to be listed for multiple companies you'll start duplicating data. To avoid that, including references from one element to another may be preferable.

XML technology has some support for references (e.g. you can use IDs and IDREF attributes and verify their correct use with a DTD) but not much, and it certainly doesn't tell to you how and where to use such references.

reinierpost