XML Novice Question

T

The Gnome

Hi I'm receiving an XML feed from one of our business partners that I
need to import into a database. I'm using XMLSpy and it really is
pretty straight forward, but I think the xml I'm receiving has unneeded
extra tags, thus I'm getting extra unneeded tables created.

Example:

<Reports>
<Report>
<Report Num>1</Report Num>
<Items>
<Item>
<Item Num>1</Item Num>
</Item>
<Item>
<Item Num>2</Item Num>
</Item>
</Items>
</Report>
</Reports>

Am I correct in thinking the "container" tags are superflous (i.e.
"Reports", "Items")? I think this is more correct:

<Report>
<Report Num>1</Report Num>
<Item>
<Item Num>1</Item Num>
<Item>
<Item Num>2</Item Num>
</Item>
</Report>
 
J

Joseph Kesselman

The said:
Am I correct in thinking the "container" tags are superflous (i.e.
"Reports", "Items")?

That's up to whoever defined the XML-based language you're processing.
If the DTD/schema calls for them, you can't leave them out because
they're part of the structure of the document. Unlike browsers, XML
tools do not try to guess what you meant; it's up to you to say it properly.

<Reports>, or something like it, is definitely needed if a single
document may contain more than one report. An XML document *must* have a
single top-level element, so there would have to be something to contain
the multiple reports.

<Items> may not be strictly necessary in this case... but it really
doesn't do any harm, and it may make some kinds of processing either.

BTW, your rewritten version is broken in any case; you forgot one of the
</Item> tags. All XML elements *must* be explicitly closed.
 
S

Steve W. Jackson

"The Gnome said:
Hi I'm receiving an XML feed from one of our business partners that I
need to import into a database. I'm using XMLSpy and it really is
pretty straight forward, but I think the xml I'm receiving has unneeded
extra tags, thus I'm getting extra unneeded tables created.

Example:

<Reports>
<Report>
<Report Num>1</Report Num>
<Items>
<Item>
<Item Num>1</Item Num>
</Item>
<Item>
<Item Num>2</Item Num>
</Item>
</Items>
</Report>
</Reports>

Am I correct in thinking the "container" tags are superflous (i.e.
"Reports", "Items")? I think this is more correct:

<Report>
<Report Num>1</Report Num>
<Item>
<Item Num>1</Item Num>
<Item>
<Item Num>2</Item Num>
</Item>
</Report>

The <Reports> element is superfluous if a single XML document can
contain only one <Report> element. Ditto for <Items>. And the <Item>
elements would be also, unless it's likely that additional elements
would appear in each beyond the (bad) <Item Num> you show.

The above isn't legal anyway, since <Item Num> is not a valid element
name (spaces aren't allowed). In addition, your second sample fails to
close one of the <Item> tags -- which is allowed (despite being poor
practice) in HTML but not XML.

= Steve =
 
J

Joseph Kesselman

Steve said:
The above isn't legal anyway, since <Item Num> is not a valid element
name (spaces aren't allowed).

Whups; hate to admit I missed that. Ditto for <Report Num>. If the
example you've shown us is really what you're receiving, you need to
have a talk with whoever is generating it and get it fixed.

Consider variants such as ItemNum or Item_Num... or just Num, since you
know from context whether it's within the scope of an Item or Report.
Or, if apporpriate, consider expressing that value as an attribute:
<Report Num="1"> ... </Report>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top