Forum Moderators: coopster
Consider this example XML code:
<month value="april">
<date>29th</date>
</month>
My script defines a function that is handled later by the XML parser:
function startElement($parser, $NAME, $attrs) {
global $startTags;
if ($startTags[$NAME]) print $startTags[$NAME];
} This works fine but only displays the content between the tags (ie: the data). So I added a line in the function above to try and display the attribute also, but cannot get it to work:
print ($startTags[$attrs]); Initially I tried just "print $attrs" but it gives me the word "Array". Looking at the PHP manual it states:
Arrays are always converted to the string "Array", so you cannot dump out the contents of an array with echo() or print() to see what is inside them. To view one element, you'd do something like echo $arr['foo'].
What does this mean and how can I get the attribute to display? (Note: I'm still much of a newbie at PHP and don't understand much about programming.)
Why is april an attribute of month.. sure april is the month?
And why is <date> part of <month>?
Shouldn't month be part of date?
<date>
<day>29th</day>
<month>April</month>
<year>2003</year>
</date>
Or better still, use integer values and then you can handle multiple languages and calendars.
graham stewart: as an aside: thats some weird looking xml schema..Why is april an attribute of month.. sure april is the month?
And why is <date> part of <month>?
Shouldn't month be part of date?
I'm not sure I agree, but then XML is new to me so forgive me. The full XML file has several months listed. They are all enclosed by a general tag called "<meetings>". Now it seemed silly to me to have a month tag, then a separate one for the word listing the actual month. Maybe I'm thinking in terms of HTML classes here, but I conceived it would be best to add the name of the month (eg: "April") as an attribute to the month tag. Is this bad XML? Should attributes be served only as numbers?
Secondly, date is clearly part of month, because everything within the month tag refers to that month only. So in my full file I have date, venue, time, and so on. Why should the date come first, then the month?
Is XML not flexible to be made up as required? So long as it is "well-formed"?
Here is a sample of my code as it stands: (sorry, the forum has stripped out the tabs at the start of each line)
<?xml version="1.0" encoding="UTF-8"?>
<meetings>
<month>
<name>April 2003</name>
<meeting>
<type>Advanced Spells</type>
<agenda/>
<agenda_text/>
<venue>University of Magic</venue>
<day>Tuesday</day>
<date>29th</date>
<time>10:30 am</time>
<room/>
<map_link>http://www[snip]</map_link>
<map_text>Directions</map_text>
</meeting>
</month>
</meetings>
I hope to improve it by shortening the number of tags and introducing attributes, hence this thread.
Is XML not flexible to be made up as required? So long as it is "well-formed"?
Well.. yes and no.
XML should be written so that the structure of the XML reflects the logical structure of the data.
So a date contains a day, a month and a year. A meeting has a location, a name, a date and an agenda. And an agenda consists of multiple items.
With that in mind, heres a possible version of that schema that you might want to try...
[pre]
<?xml version="1.0" encoding="UTF-8"?>
<meeting>
<type>Advanced Spells</type>
<location>
<building>Hogwarts</building>
<room>5a</room>
<map_link>http://www....</map_link>
</location>
<date>
<day>29</day>
<month>4</month>
<year>2003</year>
</date>
<agenda>
<item>Frogs to Princes</item>
<item>Princes to Frogs</item>
</agenda>
</meeting>
[/pre] See what I mean?
<date>
<day>29</day>
<month>4</month>
<year>2003</year>
</date>
<date>29th April 2003</date><date>29/04/03</date>
<date>Tuesday</date>
Since I'm converting directly to HTML, it's easier to have the data almost the same in the XML as it will appear on screen. Using your example above, I'd have to convert the month to a word (4 => "April"), work out the suffix of the day (eg: "23" => "23rd", whilst "14" => "14th") all adding to the programming.
I see what you mean about 'date' defining 'month' and so on. It's just the way I chose to write it when I did "My First XML File" TM. :-)
I'll get better at it!
it introduces much more code bloat
Yup, thats XML for you. The more flexible you make it the more bloated it gets. The formats you suggest only make sense in your culture, they would cause problems when exchanging data internationally between different systems (which is the general idea behind XML).
I'd have to convert the month to a word (4 => "April"), work out the suffix of the day (eg: "23" => "23rd", whilst "14" => "14th") all adding to the programming.
Thats what XSL [w3.org] is for.