Note: This blog has been deprecated, because the system it's built upon (MovableType) was comment-spammed to the point of destabilization. This URL now exists for archival purposes. Trying to add a comment to an old entry will not work here; however, the entries do exist at my blog's current manifestation, here, and comments do work (and I'm still very happy to read them, if you're so kind to leave them).
For those who understand relational databases and XML data storage: This is an exercise, and plight, in understanding primary-foreign keys in the former translating to IDs and IDREFs in the latter.
Actually, the plight isn't in translating the actual keys to ids; the difficulty I'm experiencing is making use of the relationships between the IDREFs and the IDs to which they refer. The difficulty is in using the Microsoft XMLDOM to develop the right XPath expressions. For the example, say we have this following pair of tables:
|
|
This small database represents a set of three welcome mats (primary key: Mat), with six flies (primary key: Gnat) hanging around around all of them. An XML representation of this data is kept here. The key structure is preserved via attributes; however, there is no actual enforcement on the keys as of yet. Or at least, there may not be; I'm not entirely sure if I wrote its associated Schema correctly.
The whole goal of this exercise was to see if the structure of the relational database above was preserved in its XML representation, and hopefully without having to define the id and mat attributes as IDs and IDREFs (that would mean defining a DTD or Schema for the whole document, and doing that right takes a bloody lot of effort for these two puny tables). I managed to get a solution, though I'm not happiest with it. It is a solution, in that it works: Try out the following.
See the mat for the gnat numbered:
There are buttons, the mat draws the correct values from the XML, what more could I want? Well. For one, I'd like to not do this:
var xpath_root_to_gnat = "/root/table2/gnat[@id='g" + gnatNum + "']";
var xpath_gnat_to_mat;
xpath_gnat_to_mat = "id(./@mat)";
//Before adding the inline XML scheme, the variable matNode would be null
//after trying to set it with this XPath; where the heck does the id()
//function go? It returns a node object, so shouldn't it go here?
xpath_gnat_to_mat = "//mat[@id = " + xpath_root_to_gnat + "/@mat]";
//This works, but wasn't exactly what I had in mind.
var gnatNode = xml.selectSingleNode(xpath_root_to_gnat);
var matNode = gnatNode.selectSingleNode(xpath_gnat_to_mat);
I'm glad that XPath lets me throw complete paths into the square-bracket tests; but, as the first comment reads, I'd really like to make use of the id() function rather than rely on what I think may be inefficient tree traversal. While there are only nine items in the frontier of this tree, I'm looking for code I can apply to trees with frontier of nigh twelve thousand.
I recently ordered some Kays: XSLT and XPath. If I can find a solution in there, viable for JavaScript and VBScript instead of just XSLT, I'll post a follow-up. I welcome any knowledge or advice in the meanwhile.
Tonight I learned something about how IE and FireFox differ in processing JavaScript. IE seems to wait until the document is loaded no matter what, and FireFox, well, doesn't. This answer was brought to me in less than ten sentences, here. "S M R T."
After some other tweaking, I've recrafted XSLery to be functional in FireFox. It now requires pre-rendering to view, which I don't think is as cool as just opening the XML document; but, I can live with that. I'm sure it'll make Linux people happy to see anything, much better than that blank screen.
Of course, after all that work on the code for the gallery, I don't really have any new content for it. Camping photos are pending another free weekend — I think Thanksgiving. Until then, here's the skeleton of my future galleries. The XSL, for those curious, is here.
PS: I'd be grateful if somebody could explain to me why there's a random line break in picture #13's & #16's descriptions, but no irregularities in #5's. That's probably just a puzzle for code junkies; if none of the rest of this is, at least that is.
I've finished the first version of the photo gallery I've been working on. Actually, I haven't finished it in every respect that I wish for -- there are a lot of server-side features I'd like to learn to implement, none of which I have the time for. As a result, commenting for the moment is manual; leave a comment on this blog entry and I'll move it into the XML source when I see it.
Thanks to David, I've uploaded all of the files onto FallenEarth, and you can now see the project page here, though I should give a warning: I have no clue why, but FireFox won't read my web programming right. I can fix web code at my job all right, but making something from scratch isn't yet a forte. So, to see that intro page, use a browser that shows the top as yellow on blue, which is for sure Internet Explorer 6, and possibly Opera -- I haven't done any testing in Opera. The gallery itself probably has to be viewed in IE 6 for JavaScript reasons, though I don't know why FireFox wouldn't implement the JavaScript.
I employed quite a few technologies to do this project, few of which involved the programming. Thanks to Google, Kobar and David, I found and used these programs: