This feels like bad will...Originally Posted by timvw
<a id="fruit">
<b>apple</b>
<b>tomato</b>
</a>
<a id="vegetables">
<b>tomato</b>
</a>
The b-node tomato has an a-node with id=fruit and one with id=vegetable
| SitePoint Sponsor |
This feels like bad will...Originally Posted by timvw
<a id="fruit">
<b>apple</b>
<b>tomato</b>
</a>
<a id="vegetables">
<b>tomato</b>
</a>
The b-node tomato has an a-node with id=fruit and one with id=vegetable


No bad will intended, i just didn't find it that easy human interpretable.
I admit an a-node can hold multiple b-nodes and b-nodes can have multiple a-nodes in your example. But do you see how you have copied the b-node into each a-node? Right now it may seem not that important, but imagine if each node had also multiple c-nodes. You would have to copy into each a-node that has the b-node too.. It is for that sort of redundancy we throw out the hierarchical model and choose for a relational one.
But a data-model with a lot of many-to-many relationshipst is not really a simple model. On the other had, I don't know if you ever wrote some .NET code, their datasets are all XML, and can contain many-to-many relationships.




Haven't you heard of references ?Originally Posted by timvw
and with the following DTD:Code:<a id="vegetables"> <b>tomato</b> </a> <a id="fruit" inherits="vegetables"> <b>apple</b> </a>
Code:<!ELEMENT a (b+)> <!ATTLIST a id ID #REQUIRED inherits IDREF #IMPLIED> <!ELEMENT b (#PCDATA)>![]()

This is a concept I've been working with for the past couple months.Originally Posted by hgilbert
I use PHP to generate all my content (even content out of a DB!), but instead of printing out XHTML with my dynamic content littered throughout it, I generate a XML document of all my dynamic content, then apply an XSL template that incorporates that same static XHTML. It's quite powerful and remarkably elegant, if I may say so myself.
I've created a class that will take an array (created with basically the same hierarchy that I want my XML to have) and, at its most simple form, turn it into <key>value</key>. Obviously it is significantly more capable than that, but that's the basic idea.
I'd be happy to give a better demonstration/explanation of how I've implemented this, even release my class, if there is sufficient interest. I'd like to be able to show it somewhere off the forums, but I don't have a suitable host... so if anyone can help there (either by hosting or knowing of somewhere good/free) please send me a PM![]()
NATHAN WRIGHT
PHP Developer, Simple Station


As i already posted, i figured out a solution that allowed me to model a n-m relationship in XML, the same way as it's done in a SQL table. (By introducing a reference node/table that contains the primary keys of the nodes. Notice that the redundancy has been reduced, however it's not completely gone (copying the primary key))Originally Posted by momos
A disadvantage of the XML that (afaik) it's impossible with DTD/XSD to describe the allowed values for the a- and b- attributes in the ab nodes.
Code:<a-nodes> <a id='a1'/> <a id='a2'/> </a-nodes> <b-nodes> <b id='b1'/> </b-nodes> <ab-nodes> <ab a='a1' b='b2'/> </ab-nodes>




You've got to be kidding, right ?Originally Posted by timvw
Look above at my example (if you haven't noticed it) and please note that attribute inherits is of type IDREF and every parser will issue an error if the id doesn't exist.


I like that approach too. (Although sometimes, i prefer to use one of the many available php classes that generate html)Originally Posted by Nate
Here is a MySQL2XML snippetOriginally Posted by Nate
PHP Code:while($row = mysql_fetch_assoc($result))
{
$occ = $doc->createElement('entity');
$occ = $root->appendChild($occ);
foreach($row as $fieldname => $fieldvalue)
{
$child = $doc->createElement('attribute');
$child->setAttribute('name', $fieldname);
$child = $occ->appendChild($child);
$fieldvalue = mb_convert_encoding($fieldvalue,'UTF-8', 'ISO-8859-1');
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}




@timvw
Am I invisible or something ?![]()


No, i'm not ignoring youOriginally Posted by bonefry
![]()
I'm learning.. I'm reading Chapter3 (IDREF etc) in the XML specification..
![]()




Originally Posted by timvw
Try this: http://www.w3schools.com/dtd/default.asp
It's easier to read.


Originally Posted by bonefry
Well, i've searched http://www.w3.org/TR/2004/REC-xml-20040204 and the only "references" i found where entity and character references. So, i think i haven't heard about references. Where can i hear about them?
Currently, you seem to use a "reference" (somewhat a foreign key) in the a-fruit node to the a-vegetables node. That is exactly the same i did with the a,b, ab-nodes example. So i don't know why you asked that i was kidding there?
Below is an example of XML and DTD i would like to use. I want to apply a constraint on the values of the aid and bid attributes of the ab nodes. The point is to allow only IDs from an a-node as aid value, and IDs from a b-node as bid value.
IDREF only checks if there is some node with that ID.
IDREFS doesn't seem usable, because i don't know beforehand which IDs my a-nodes and/or b-nodes will have.
Feel free to provide a DTD that allows me to specify thisCode:<?xml version="1.0"?> <!DOCTYPE root [ <!ELEMENT root (a-nodes, b-nodes, ab-nodes)*> <!ELEMENT a-nodes (a*)> <!ELEMENT b-nodes (b*)> <!ELEMENT ab-nodes (ab*)> <!ELEMENT a (#PCDATA)> <!ELEMENT b (#PCDATA)> <!ELEMENT ab (#PCDATA)> <!ATTLIST a id ID #REQUIRED> <!ATTLIST b id ID #REQUIRED> <!ATTLIST ab aid IDREF #REQUIRED bid IDREF #REQUIRED> ]> <root> <a-nodes> <a id='a1'>hihii</a> </a-nodes> <b-nodes> <b id='b1'/> <b id='b2'/> </b-nodes> <ab-nodes> <ab aid='a1' bid='a1'/> </ab-nodes> </root>
Btw, as XSD was also allowed as scheme, i think it's possible to declare the constraint with <xs:restriction>.




Dude, you want too much validation. These kind of validation it's better off done directly in PHP or XSLT.Originally Posted by timvw
EDITED: the DTD or XSD validation are usefull only to validate syntax, and not logical mistakes

This is significantly more complicated than it needs to be.Originally Posted by timvw
Heres what mine looks like.
That generates XML of identical form. The class that I'm using can also handle attributes and multiple repetitions of the same tag (shown in the way that new elements of $out['entry'] are created).PHP Code:$out = array();
$out['entry'] = array();
while($entry = $db->fetch_array($entry_temp))
{
$out['entry'][] = array(
'id' => $entry['id'],
'topic' => $entry['topic'],
'date' => date('D j M Y', $entry['datetime']),
'time' => date('H:i', $entry['datetime']),
'comments' => $entry['comments'] . ' Comments',
'body' => $entry['body']
);
}
You tell me which is cleanerHTML Code:<entry> <id>6</id> <topic>The First Entry</topic> <date>Tue 2 Aug 2005</date> <time>15:50</time> <comments>4 Comments</comments> <body>This is an example entry.</body> </entry> <entry> <id>7</id> <topic>The First Entry</topic> <date>Tue 3 Aug 2005</date> <time>18:27</time> <comments>0 Comments</comments> <body>This is another example entry.</body> </entry>![]()
NATHAN WRIGHT
PHP Developer, Simple Station
rikshot, u r very right in that you dont really need to use XML for what you are doing... the advantage of xml is:
your separate your content "completely"!.. e.g. here is an xml file that holds content for ur company:
<company>
<name>2Cool Web Design</name>
<phone>123-456-789</phone>
</company>
now you can use this single data source in several applications at the same time.... e.g. a flash site thats shows ur phone number... an html site showing ur phonu number ... then goes your WAP site.. so the basic purpose is to create a single easy to edit data file that will be useful in the future where several different devices will be using the net and visiting your site...
the content will be there.. the mediums of delivery change..
currently (in XHTML), you do not have too much customization to allow your content to work everywhere...
But say, if you are to accept orders via phone... and use VocieXML... then again.. XML rocks!..
i hope my long speech was somewhat useful
regards,
Kamran.
XML is about exchanging data and presenting it in a neutral format that can be understood by many devices and applications. Sure, it's also good for storing small amounts of data records, but not for many many records, that's why we have relational databases. Think of XML as at the middleware that sits between a proprietary system and newer systems, or as a translator between yourself and a non English speaker. It's about data interchange, rather than data storage.
Karl Austin :: Profile :: KDA Web Services Ltd.
Business Web Hosting :: Managed Dedicated Hosting :: From £250/m
Personal Web Hosting :: Budget Web Hosting :: From £50/y
Call 0800 542 9764 today and ask about our Cloud Hosting Platform
The future will show us what XML is meant for... at the moment it is one of those buzzwords, which makes people think it is good for everything...




I was watching "Judging Amy" on TV yesterday, and Amy said:Originally Posted by R. U. Serious
That's about as profound as any other statement about the "next big thing".If I don't stop [eating this], my butt will be the next big thing.![]()
Dagfinn Reiersøl
PHP in Action / Blog / Twitter
"Making the impossible possible, the possible easy,
and the easy elegant" -- Moshe Feldenkrais





Well, how would you do the following, when you have another level to deal with, as such as this for example, via your array implementation?You tell me which is cleaner
I would think that generating the tags on the fly is the better approach, more cleaner and easier to implement? An alternative, would be to build up the XML as a string instead, then create a DOM object using that stringCode:... <author> <firstname> ... </firstname> <lastname> ... </lastname> ...</author>![]()


I would really avoid this. If you are making an XML object, use the DOM. Creating XML from strings leads to issues like character encoding and such.Originally Posted by Dr Livingston
Unless you write your own encoding algorithm. Which would be a bit of a waste, as well as be much slower than just using the DOM in the first place.




What makes you think the DOM implementation in PHP doesn't have bugs ? In fact the first versions of the new libxml2 based parser, when I last tried to work with unicode, it froze when trying. And I documented myself and found it was a well known bug. It's stupid to generate XML with DOM simply because it is more time consuming and error-prone (yes, due to bugs, they always have bugs in PHP).Originally Posted by wwb_99
Also, when creating XML with DOM, aren't you using strings anyways ? (for the values and field names and all that). What changes ?


XML is not a big string that happens to be valid XML. Xml is an in memory object expressed through the dom that happens to be easily serialzable into a string representation. It is made up of trees, nodes and leafs not character data.
PHP DOM implementation issues aside, there are significant issues with generating Xml through string concatenation.
1) The biggest one is character escapes and encoding. Now, this can be mitigated somewhat by using CDATA nodes. But that is an ugly kludge at best. What it leads to is that one need write a perfect string scrubbing function. Which is difficult to get perfect and grossly inefficent in anything short of C.
2) The onust of maintaining valid Xml structures falls onto the application, not the Xml library. For simple stuff, like most of the above examples, it is not too tricky. But get something complex and real-world and you can have alot of trouble with this.
3) Given that the DOM implementation, even if it has some bugs, is written in compiled machine code rather than interperted PHP, it can handle one and two alot better than your script can. Furthermore, since all it is concerned with doing is handling Xml in a proper and valid manner, I would bet on it having less bugs than a custom Xml implementation.
Bottom line: Xml is not a string. Don't treat it like one.





I was looking at the php internals list, and it looks like they are going to vote on including the XMLWriter extension in the main php dist.
Eric Coleman
We're consentratin' on fallin' apart
We were contenders, now throwin' the fight
I just wanna believe, I just wanna believe in us




You are mistaking XML for DOM. There are other ways to parse it you know, like SAX. And it's an object only if you look at it like that. Because you can also look at it as a relational table for example.Originally Posted by wwb_99
The only encodings necessary are < that becomes a < and & that becomes an &Originally Posted by wwb_99
You give compiled code too much credit. What can be more faster than a simple echo ?Originally Posted by wwb_99


We are talking about writing Xml, not reading Xml here. SAX is great for reading. In fact, most DOM implementations use a SAX reader to build the DOM tree.You are mistaking XML for DOM. There are other ways to parse it you know, like SAX. And it's an object only if you look at it like that. Because you can also look at it as a relational table for example.
IIRC, alot more encodings are necessary. Like ". Then you get into funky characters that don't really belong but appear. Like crap from Word.The only encodings necessary are < that becomes a < and & that becomes an &
A compiled echo:You give compiled code too much credit. What can be more faster than a simple echo ?
Code:#include <stdio.h> int main(void) { printf("Hello World!\n"); exit(0); }
Bookmarks