Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] HTML + XML problem

Started by Vanderry Jul 1, 2010 at 6:23 PM 5 replies 1k views
Original Post
Vanderry
Vanderry
Hey folks !

I've tried to incorporate XML into my website for the sake of modularity and I have come to understand &#106avascript is the way to do this. However for some reason I'm having a real hard time making this work in my own websites, even when I use the exact same code and files provided in this tutorial (first example).

When I use the "TryIt Editor" they provide things look just the way they should, but as soon as I use files on my own computer or online server then the script seems to halt at "xmlhttp.open("GET","whatever.xml",false);". Any other code below this point, html or otherwise, isn't displayed. The problems are so incredibly irregular as I managed to display the sample code and xml-file (on my computer) 1 time in Internet Explorer, but after that it didn't work anymore (I tried to clear the cache). Firefox doesn't seem to have this problem but then it won't display the page on my server at all.

I'm going nuts with this problem because it is so unrewarding to try to solve it when there is no direct error feedback. If someone could give me a hint what the problem might be or if there is an alternative to using &#106avascript I would be ever grateful.<br><br>- Dave
leiavoia
leiavoia
I'm not understanding your use of XML. Why did you choose to use it in this case?

If your intention was to serve (X)HTML to the end user, in my experience it is easier to simply build XHTML to begin with.

If you have backend storage in XML (documents, for instance), then it makes sense to try to deal with with directly. In this case, a decent strategy is to use XSLT to transform the XML into XHTML for web consumption. This basically maps all XML elements to something in the XHTML domain. You can also organize, sort, filter, or mangle the XML during transformation if you just want to get a certain subsection or selection. So anyway, XSLT is the key technology i think you are looking for. Most browsers will even transform XML on the fly these days if supplied with the correct XSLT stylesheet.

However, be warned: XSLT is ugly, verbose, and very unfriendly (hope you love recursion!). If you have no business messing with XML is the first place, i strongly recommend you stick to regular XHTML.
Vanderry
Vanderry
Yes, I think XSLT might be the technique for me. What I've been trying to do is establish an image gallery by storing and loading thumbnail, image, name and description for each entry in an XML file. When I decided to separate this data from the HTML files XML was just the first thing I could think of so I understand there might be other suitable storage formats.

Having looked briefly at this tutorial I do like the simple syntax and that it looks easy to integrate the XML action into HTML. However what I don't quite understand is where in the code the XML file is referenced (I don't see any file name anywhere). Also, the .xsl file doesn't seem to be directly readable by a browser, do I need a third file in another format to properly display the page?

Many thanks for taking the time to read and answer my post, I hope these subsequent questions aren't too cumbersome x)
Vanderry
Vanderry
Ok, so I get XSLT provides a means to display XML-files according to a an associated style sheet. This is a pretty neat thing, although I was hoping to be able to use the data from my XML file in different pages rather than tying it to one format.

To be more specific about my implementation, I have a gallery page where the image thumbnails are displayed in a table-like fashion. By clicking any of these you are taken to a closeup-view page where the full size version of the image is shown. There are buttons to display the previous or next image as they are ordered in the XML-file. Here is what the XML structure looks like:

<gallery>     <artpiece>          <name>Demon Soldier</name>          <thumbnail>DemonSoldier_Mini.jpg</thumbnail>          <picture>DemonSoldier.jpg</picture>          <description>Blablabla...</description>     </artpiece></gallery>

So what I do for the most part is extract file names which are then used along with the tag to display the right image. I haven't figured yet how this would be done using XSLT. In &#106avascript at least it seemed possible to extract the file name and then use with document.write() to construct the tag dynamically.
leiavoia
leiavoia
First of all, if this is your own website, this problem is an issue best suited for a database. If it's a very small thing and you intend this XML to be a sort of "config file" for a page to display, then it might be appropriate. If that's the case, XSLT is not for you. You want to translate the XML directly to something your script of choice can handle by using any number of XML parsers.

Although called a "stylesheet," XSLT is NOT responsible for the styling of XML. That's the domain of XSL-FO or CSS. Rather, XSLT is used to transform XML into some other data format, including more XML. XSLT is ideal for translating a 300 page book that looks like this:



Name of Chapter

Some text . . .



Into...




Name of Chapter

Some Text . . .






Or even LaTeX, plain text, DocBook, or whatever other format you need.

XSLT is also used to traverse and manipulate XML. For instance, you could use it to extract only chapter 1 from the book, or choose items from a list based on some condition.

For your needs however, you are probably better off just getting right at the data, config-file style. XSLT works better at processing gobs of data.
Vanderry
Vanderry
Perhaps it is time I take a look into database management. Thank you for a very insightful explanation of XSLT and the alternatives, leiavoia :) Can I just ask one more thing though, is there any way an HTML image tag could be constructed with the source file name fetched dynamically through XSLT? Or in other words, can XSLT be used to establish anything within the HTML tags or does it need to be explicitly between them?
leiavoia
leiavoia
XSLT can translate XML into any arbitrary format. However, it's particularly well suited for markup like XML or XHTML. So to answer your question: yes, easily.

Try the XSLT tutorial. Not only will you learn something, but it's very similar to what you want to do. If browser-based translation is not what you want, try a command line tool. I prefer XML Starlet

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.