Understanding XSLT transformations: Your Own `XML Transformation Utility` - Understanding the XML and XSLT
(Page 2 of 6 )
With in the previous section, I defined “sample1.xml” as following:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Sample1.xsl"?>
<greeting>
Hello, World!
</greeting>
The first line is just a processing instruction to an XML parser (in simple terms a “parser” is similar to an “interpreter” in old technical terms) that the content of the file is based on XML standards of version “1.0” and so on.
The second line informs the XML parser that the XML document needs to link (or transform) with “Sample1.xsl” at run time. All the lines after the second line are actually the customized and user-defined structure of the XML data (or information).
Coming to “sample1.xsl”, I shall explain part by part. Let us consider the following first:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
………
</xsl:stylesheet>
I think you are already clear about the first line. Coming to the next structure (xsl:stylesheet), our entire transformation (“format” for “presenting” xml) lies in between the opening and closing tags of “xsl:stylesheet”.
The next interesting statement would be the following:
<xsl:output method="html"/>
I am just informing to the XSLT transformer that the format should confirm to the rules and regulations of “html”. Other values would be “text” or “xml”. Further proceeding we have the following:
<xsl:template match="/">
………
</xsl:template>
This is the heart of our transformation. The parser tries to emit the content (or format) existing within those two tags when it is working on the “root” node of the XML document. The “root node” is different from the “root element”. This is the cause of much confusion among several programmers. The “root element” of the above XML document is “greeting”. But, the “root node” for any XML document (in general) would always be “/”. The “Node” refers only to the path but “element” refers only to element(s) present within the XML document.
From the above, when a root node is found by the parser, it transforms XML to the content defined with the matching based on “match="/"”. The transformation content (or template) is defined as follows:
<html>
<body>
<b>
<xsl:value-of select="."/>
</b>
</body>
</html>
So, our transformation starts with HTML, BODY and B tags first. The following line returns all of the text of “nodes related to current context (which is root node) and all sub nodes of current context”. In this case, our XML document simply has only one “greeting” element at the root node. And thus it returns “Hello world” in between bold tags surrounded by BODY and HTML tags.
Next: “XML transformation utility,” is it really necessary? >>
More XML Articles
More By Jagadish Chaterjee