Jump to content

Definitions in XML


Glom

Recommended Posts

I have a glossary page on my website. There are over a hundred definitions in it. I started reading through the XML tutorial and figured it would be possible to store the data in an XML file and pin it in the XHTML file.The idea was to have the name of the object, its definition and an anchor for quick linking. This is what I came up with.

<xml id="glossary" src="glossarydb.xml"></xml><dl datasrc="#glossary"><dt><a name="<span datafld="LINK"></span>"></a><span datafld="NAME"></span></dt><dd><span datafld="DEF"></span></dd></dl>

It didn't work. I know the anchor looks a bit messy, but I also tried it with the anchor part commented out. Still didn't work. I then tried it using a table format instead, like the example in the tutorial and that worked. How would I do this with the definitions tags?Then the question is, would making the glossary using XML have any advantage over just doing it in the XHTML as I currently do?

Link to comment
Share on other sites

Well, the first thing you must understand is that Data Islands are not supported in browsers other then IE. After ensuring that's not a problem, note that XML files must be fully loaded before processed. If you're going to store big dictionaries, using XML on the server side or MySQL is a better solution. If you're still brave enough to go to the wrong way of data islands...Does your XML have a consistand naming scheme? For example:

<glossary><definition><LINK>something.html</LINK><NAME>A</NAME><DEF>This is something</DEF></definition><definition><LINK>smething-else.html</LINK><NAME>AA</NAME><DEF>This is something else</DEF></definition></glossary>

Also, note that XML is case sencetive. Are your <NAME> and <LINK> elements in the XML file written in upper case letters as witnin the XML?A better way to "link" XML data to XHTML would be XSLT, but that's definetly not as easy as the Data Island method. It does however give you a lot more benefits and it's where XML comes in handy.The advantage of storing data this way is that it's reusable. You have to edit only that data for it to appear everywhere it's used. And by everywhere I don't only mean every page but every platform as well.

Link to comment
Share on other sites

A better way to "link" XML data to XHTML would be XSLT, but that's definetly not as easy as the Data Island method. It does however give you a lot more benefits and it's where XML comes in handy.
XSLT it is then. A quick play around and I'm definitely having more luck with that (as much as possible having just learnt the basics 20 minutes ago). It definitely doesn't warm to the idea of trying to use value-of tags to gives attributes of other tags like img though. That's a problem because my other thought for the use of xml is in my template for the nav bar links.
Link to comment
Share on other sites

Oh... at times like this I wonder when is W3Schools going to add the <xsl:variable> and <xsl:attribute> explanations. The lack of them makes XSLT look like a weak language when infact it's a lot more powerful.In order to add attributes you could use those two elements in one (or more?) of theese fashions:If the attribute always exists and it's name is known, the simplest method is

<xsl:variable name="href" select="LINK"/><a href="{$href}"><xsl:value-of select="NAME"/></a>

If the attribute doesn't always exist and/or it's name is unknown, you could use something simmilar to:

<a><xsl:attribute name="href"><xsl:value-of select="LINK"/></xsl:attribute><xsl:value-of select="NAME"/></a>

Note: the currently represented situation is going to produce the same output. Adding conditionals and variables to this however would be useful for the situation I'm talking about.

Link to comment
Share on other sites

Hey that worked great.  But how do I put this xml file into my HTML file?

There are three ways:1. Don't put it inside the (X)HTML. Make the whole page(s) with XSLT(s) and XML(s) and run the XML(s) to view the result. The most bumpy road which I wouldn't suggest relying much on.2. Use a server side script like the one on the "XSLT on the server" example at the desired spot where the XSLT transformation should be perform. You'll get the actual output of the XSLT this way. A combination of this and the previous method (use a server side script to execute a whole XSLT&XML based site) is the best method in my opinion and it is infact what some sites are doing.3. Use this JavaScript code to execute the XSLT in a desired div container. You won't get the source code and the XML data is not that searchable (by search engines I mean) scince you're using script to import it. I'm having difficulties using two copies of this script but still: it works.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...