Lesson 6. Working with XML

<?xml version="1.0" encoding="windows-1251" ?>
<article>
    <author id="
1" />
    <title>
Lesson 6. Working with XML</title>
    <body>
        <para>
Imagine, you are allowed to invent any tags
          with any attributes. That means, you can define
          by yourself what a tag or attribute that you
          invent means.
</para>
        <para>
Such a code will contain data, …</para>
    </body>
    <links>
        <link href="
http://www.parser.ru/docs/lang/xdocclass.htm">Class xdoc</link>
        <link href="
http://www.parser.ru/docs/lang/xnodeclass.htm">Class xnode</link>
    </links>
</article>

…but not the formatting. One person can handle preparing data and another-formatting. What they need to do is just agree on the tags they are going to use and get down to work over the project... simultaneously.

This idea is no news. There were many template-processing libraries, and many developers created yet more libraries of their own. Libraries were incompatible and totally dependent on scripting languages used, which caused dissociation among developers and made an outsider spend lots of time and efforts on learning yet another library.

Life goes on though, and now we have standards XML and XSLT, which do not depend on scripting language chosen and allow us to fully implement the idea we have shaped in the beginning. We also have standards DOM and XPath, which reveal yet more opportunities. All these standards are fully supported in Parser.

While working over this lesson, open a book describing XML and XSLT (the one you bought in the nearest bookstore yesterday) and use it as a reference.

Let's see how we could transform the above XML-coded article to HTML. First, we place the above given code into file
article.xml and then create file article.xsl, where we define the tags we have invented:

<?xml version="1.0" encoding="windows-1251" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="article">
    <html>
        <head><title>
<xsl:value-of select="title" />
</title></head>
        <body><xsl:apply-templates select="body | links" /></body>
    </html>
</xsl:template>

<xsl:template match="body">
    <xsl:apply-templates select="para" />
</xsl:template>

<xsl:template match="links">
    Related links:
    <ul>
        <xsl:for-each select="link">
            <li><xsl:apply-templates select="." /></li>
        </xsl:for-each>
    </ul>
</xsl:template>

<xsl:template match="para">
    <p><xsl:value-of select="." /></p>
</xsl:template>

<xsl:template match="link">
    <a href="{@href}"><xsl:value-of select="." /></a>
</xsl:template>

</xsl:stylesheet>

The data and the transformation template are ready. Now we should create
article.html, in which we write:

input xdoc document
$sourceDoc[^
xdoc::load[article.xml]]

transform xdoc document using template article.xsl
$transformedDoc[^sourceDoc.transform[article.xsl]]

output the result as HTML
^transformedDoc.
string[
    
$.method[html]
]

The code in the first line loads XML-file and gets its DOM-interpretation in
sourceDoc. The construction is like that loading a table-remember ^table::load[...]? Yet, this time we do load NOT a table (thus getting an object of class table) but XML-document (and get an object of class xdoc).

The code in the second line makes input document subject to transformation according to the template defined in
article.xsl.

The code in the third line outputs the resulted document as HTML (parameter
method with value html).

In this method, we can specify all parameters allowed for
<xsl:output … />.
We also recommend that you specify "no indents" (parameter
indent with value no: $.indent[no]) to avoid widely-known problem with empty space in front of </td>.

Now, when accessing this page, a visitor will get the result of the transformation:

<html>
<head><title>
Lesson 6. Working with XML</title></head>
<body>
<p>
Imagine, you are allowed to invent any tags
          with any attributes. That means, you can define
          by yourself what a tag or attribute that you
          invent means.
</p>
<p>
Such a code will contain data, ...</p>
Related links:
<ul>
<li><a href="
http://www.parser.ru/docs/xdocclass.htm">xdoc class</a></li>
<li><a href="
http://www.parser.ru/docs/xnodeclass.htm">xnode class</a></li>
</ul>
</body>
</html>


As you have probably noticed, tag
 <author ... /> was not defined, so the information on the author of the article is not present in resulting HTML. Later, when you decide where you will store and output the data on authors and how you will do it, you will just need to complete the template without changing articles' content.

Note: if you don't want Net surfers to view your
.xml and .xsl files, you should either store these files beyond web-space (^xdoc::create[/../directory_outside_of_web_space/article.xml]) or disallow access to these files by your web-server directives (an example of how to disallow access to .p files can be found in "Appendix dedicated to installing Parser on Apache web-server").

Let's sum it up,

What have we done?
We have created a "building block" to be further used for retrieving information stored in XML, applying XSLT-transformation, and outputting objects in HTML format.

What have we learnt?
·how to use class xdoc;  
·how to load XML, create XSLT, and use it to transform XML and output objects of class xdoc as HTML.  
 
What should we remember?
You should buy a book on XML and XSLT.

What's next?
You should read the book we've mentioned, experiment with examples it gives, and enjoy good standards. You should also read about method postprocess and find a way to tune it up so that every access to XML-file would output it as HTML.



Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 22.06.2004