Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
XML

Posted by Garrie (Garrie), 7 October 2002
Can anyone recommend a good place to start learning about XML?
I've looked at a number of websites and I am still confused about how to use XML. In particular, how to use XML to generate dynamic webpages.

- Garrie

Posted by John_Moylan (jfp), 7 October 2002
A good primer(s) for XML can be found at:
http://www.topxml.com/xml/learnxml.asp

XML is a way of saving your content/data in a structured form within a text doc or database.

You then parse the XML tags to extract the relevant data, what you do with it is up to you, but as you mentioned in your post generating web pages is a popular use for XML.

eg: extract your XML data and sent it to an html template to build and deploy your html page.
(This is a separation of content, logic and presentation, and is a good thing)
XML = Content
Programming lang = Logic
presentation = Template


The bit that does the parsing and building could be wriiten in any language, popular examples being "Perl, php, java".

You really need to decide on a language before specifics can be given on generating dynamic content, but php would be a nice easy route. (www.php.net)

jfp

Posted by Garrie (Garrie), 7 October 2002
Thanks I'll have a look.
It will probably be PHP I'll use to generate the pages.
So any help along those lines will be appreciated.

- Garrie


Posted by John_Moylan (jfp), 7 October 2002
Then I'll point you towards some urls and a good book.

I've not personally used the sites tutorials as I prefer my tutorials to come from tree's.
Url's:
http://www.zend.com/zend/tut/tutbarlach.php
http://www.analysisandsolutions.com/code/phpxml.htm

Books:
Beginning PHP4, published by WROX (make sure its PHP4, there is a older version of the book still being sold) is a good primer for php and includes a section on XML parsing.
No talk on templating here though, but I suggest you look at http://smarty.php.net/ for starters, there is another called fast template but smarty is faster and more extendable.

Have the appropriate amound of fun.

jfp

Posted by admin (Graham Ellis), 8 October 2002
For formatting data that's held in XML format, you probably want to have a look at XSLT (XML Stylesheet Language Transforms).

An XSLT document is a set of directives telling a formatter how to handle data found in an XML document, and it's written in XML itself, using a specific tag set that ensures that it's XSLT.

Perhaps an example would help?

Here's a file of XML:
Code:
<?xml version="1.0"?>
 <dromedaries>
   <species name="Camel">
     <humps>1 or 2</humps>
     <disposition>Cranky</disposition>
   </species>
   <species name="Llama">
     <humps>1 (sort of)</humps>
     <disposition>Aloof</disposition>
   </species>
   <species name="Alpaca">
     <humps>(see Llama)</humps>
     <disposition>Friendly</disposition>
   </species>
</dromedaries>


Here's the XSLT that I might use to translate it into HTML:
Code:
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
 <html>
 <head><title>Know Your Dromedaries</title></head>
 <body>
   <table bgcolor="#eeeeee" border="1">
   <tr>
   <th>Species</th>
   <th>No of Humps</th>
   <th>Disposition</th>
   </tr>
   <xsl:for-each select="dromedaries">
     <xsl:apply-templates select="./species" />
   </xsl:for-each>
 </table>
 </body>
 </html>
</xsl:template>

<xsl:template match="species">
 <tr>
 <td><xsl:value-of select="@name" /></td>
 <td><xsl:value-of select="humps" /></td>
 <td><xsl:value-of select="disposition" /></td>
 </tr>
</xsl:template>

</xsl:stylesheet>


And here's a Perl program to do the conversion:
Code:
use XML::LibXSLT;
use XML::LibXML;

my $parser = XML::LibXML->new();
my $doc = $parser->parse_file("camels.xml");
my $style_doc = $parser->parse_file("camels.xsl");

my $xslt = XML::LibXSLT->new();
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($doc);
my $output = $stylesheet->output_string($results);

print $output;




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho