Jump to content
  • Advertisement
Sign in to follow this  
Vanderry

[web] HTML + XML problem

This topic is 3089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey folks !

I've tried to incorporate XML into my website for the sake of modularity and I have come to understand javascript 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 javascript I would be ever grateful.

- Dave

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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 <img> tag to display the right image. I haven't figured yet how this would be done using XSLT. In javascript at least it seemed possible to extract the file name and then use with document.write() to construct the tag dynamically.

Share this post


Link to post
Share on other sites
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:

<book>
<chapter number="1">
<header>Name of Chapter</header>
<paragraph>Some text . . .</paragraph>
</chapter>
</book>

Into...

<html>
<body>
<div id="chapter_1">
Name of Chapter</h1>
<p>Some Text . . .</p>
</div>
</body>
</html>

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!