Friday 17 December 2010

Task 7: Discuss how XSL can be used to generate XHTML, with examples

XSL stands for EXtensible Stylesheet Language. XSLT stands for XSL Transformations. XSLT is used to convert XML documents to other formats like HTML,word etc.It is a W3C standards.
There are three major parts of XSL

1) XSLT- to transform XML document,
2) XPath - to navigate through XML
3) XSL-FO- to format XML document

XSLT is more importnat for us.XSLT is used to transform XML document to another type of document like HTML. It is supported by all major browsers. Consider CSS( cascading style sheet) is used to transfom HTML document. It is used to give look and feel to to HTML tags. Consider below example.

span
{
background-color:red;
}

The above example gives red back ground color to each span tag. Same way XSLT is used for XML document. Consider above company information XML. If we directly view this XML file in browser, It does not look good. We can use XSLT to transform this XML document. Like If I want to display company name in bold in allign it to center. We can use following XSLT code.

<xsl:template match="CompanyName">
<tr align="center">
<th>
<xsl:apply-templates/>
</th>
</tr>
</xsl:template>


It will display CompanyName in bold and allign it to center.

XPath

XPath is a query language for locating nodes of an XML document. It is based on the W3C XML Path Language specifications for a general-purpose query language for addressing and filtering elements and text of XML documents. XPath expressions can address portions of an XML document; it can manipulate strings, numbers, and Booleans; and it can match a set of nodes on content values Furthermore XPath indicates nodes by position, relative position, type, content, and several other criteria. An XML document is a tree made up of nodes. Some nodes contain one or more other nodes. There is exactly one root node, which ultimately contains all other nodes. XPath is a language for picking nodes and sets of nodes out of this tree. From the perspective of XPath, there are seven kinds of nodes: The root node, Element nodes, Text nodes, Attribute nodes, Comment nodes, Processing-instruction nodes, Namespace nodes.

XSL As I mentioned above XSL can be used to create XHTML by adding XSLT as its core process. For example a document does not transform as such but a fresh document can be created from the innovative document. In the conversion process XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML  PDF, PostScript in using XSL-FO AWT, PNG  XHTML and etc. Normally XSLT does this by transforming each XML element into an (X)HTML element. Also xml documents can be formatted for number of other documents 

The declaring of  XSL file into a XML documents is quite not hard, in accordance to my simples term, here is the illustration of how it can be declared in the heading section of a document. And another important factor is that all the XML source documents and XSL sytlesheets have to be in the same domain to avoid complication which might not happen if processed in the right way.


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Books.xsl"?>


Below is the illustration on how XML document transforms into XHTML by using XSL. Moreover the codes below correspond to source code for the XML document which will be transformed into XHTML.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
  <cd>
    <title>Emergency</title>
    <artist>Tank</artist>
    <country>USA</country>
    <company>Atlantic Records</company>
    <price>9.99</price>
    <year>2010</year>
  </cd>


This is the XSL style sheet named XSLTransform.xsl that describes how to transform the XML. The conversion creates <root> as the root-enclosing element and <company> as the second-level repeating element. Each <company> element in the output XML document will include the values from the <cd> element and the company= attribute from the input XML document. 

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/vehicles">
       <root> <xsl:apply-templates select="cd"/> </root>
</xsl:template>
<xsl:template match="cd">
        <company="{@company}">
           <xsl:value-of select="company" />
        </company>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>

No comments:

Post a Comment