PYTHON  
 
  Oracle Forms & PL/SQ  
 
  ROR  
 
  Java  
 
  php  
 
  ask us  
 
  home  
 

 

Web Technologies






XSL Languages

1. Preface

How to display TEXT by a browser?
Write TEXT into HTML, better: XHTML
Just that?
better: use CSS


How to display XML by a browser?
Transform XML into XHTML
How to do?
use XSL.


XSL stands for EXtensible Stylesheet Language. HTML uses CSS as a style sheets; XML uses XSL instead. XSL is the XML Style Sheets. It contains XSLT for transforming XML documents, XPath for navigating in XML documents, and XSL-FO for formatting XML documents.

XSLT transforms an XML document into another XML document recognized by a browser: Transformation here does more than what It does a CSS sheet styles. XSML transforms a source XML document into HTML and XHTML (by transforming each XML element). It allows elements and attributes to be added or removed from the output XML file. We have also the possiilities to rearrange and sort elements, and more. Currently, the major browsers are available with an XSLT implementation.


2. XML document:

We consider the following XML document.
Example:

a. Saved as atoms.txt (the raw XML source document) a href = "atoms.txt" atoms.txt.
"View Source" from the browser menu will display the same style document.


b. Saved as atoms.xml, without XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
a href = "atoms0.xml" atoms.xml


The plus (+) and minus sign (-) to the left of the elements can be clicked to expand or collapse the structure of the element.
b. Saved as atoms.xml, including XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="atoms.xsl"?>


3. XSL Document:

An XSL document contains:

3.0. <?xml version="1.0" encoding="ISO-8859-1"?>



3.1. A root element: Style Sheet Declaration

writen at the top of the document, according to W3C namespace,
like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
(use stylesheet or transform: they are the same)

3.2 Elements:

3.2.1. Template elements <xsl:template> It contains rules for nodes. Its attribute "match" is used to associate this template to the XML element, or to the whole XML document (specified in XPath by "/").
Example:
View the output



3.2.2. <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.
The <xsl:value-of> Element
This element is used to extract values from an XML element and add them to be displayed in the output stream of the transformation with an XHTML document.
Example:

<ATOMS>(the root)
-<NOBLEGAS> (first child)
(and the siblings)
<NAME>Helium</NAME>
<SYMBOL>He</SYMBOL>
<CHARGE>2</CHARGE>
<MASS>4.003</MASS>
</NOBLEGAS>


In atoms.xml file, we can then extract the following:
<tr>
<td><xsl:value-of select="ATOMS/NOBLEGAS/NAME"/></td>
<td><xsl:value-of select="ATOMS/NOBLEGAS/SYMBOL"/></td>
<td><xsl:value-of select="ATOMS/NOBLEGAS/CHARGE"/></td>
<td><xsl:value-of select="ATOMS/NOBLEGAS/MASS"/></td>
</tr>

View the output



3.2.3. <xsl:for-each> Element This element loops through the XML elements,
and display every XML element.


<xsl:for-each select="ATOMS/NOBLEGAS">
<tr>
<td><xsl:value-of select="NAME"/></td>
<td><xsl:value-of select="SYMBOL"/></td>
<td><xsl:value-of select="CHARGE"/></td>
<td><xsl:value-of select="MASS"/></td>
</tr>
</xsl:for-each>
a href = NobleGases3.xml

Remark:
Inside an <xsl:for-each> element, we can make some selections like filtring by not to display the Xenon noble gas in the atoms.xml:
<xsl:for-each select="ATOMS/NOBLEGAS/SYMBOL [SYMBOL !='Xe']">
...

</xsl:for-each>
View the output



3.2.4. <xsl:sort> Element It sis used to sort the output. Its place is between
</xsl:for-each> and </xsl:for-each>, like this:


<xsl:for-each select="ATOMS/NOBLEGAS">
<xsl:sort select="MASS"/>
<tr>
<td><xsl:value-of select="NAME"/></td>
<td><xsl:value-of select="SYMBOL"/></td>
<td><xsl:value-of select="CHARGE"/></td>
<td><xsl:value-of select="MASS"/></td>
</tr>
</xsl:for-each>
View the output



3.2.5. <xsl:if> Element Its syntax is:
<xsl:if test ="expression">
Do something ...
</xsl:if>
Its place is inside the <xsl:for-each> element
in the XSL file:
<xsl:for-each select="ATOMS/NOBLEGAS">
<xsl:if test ="MASS < 20">
<tr>
<td><xsl:value-of select="NAME"/></td>
<td><xsl:value-of select="SYMBOL"/></td>
<td><xsl:value-of select="CHARGE"/></td>
<td><xsl:value-of select="MASS"/></td>
</tr>
</xsl:if>
</xsl:for-each>


View the output



3.2.6. <xsl:choose> Element The element choose warps two other elements:
<xsl:when> and <xsl:otherwise>.
The <xsl:choose> element is used in conjunction with <xsl:when>. It expresses multiple conditional tests. It resembles to " if () else{}".
Here is an Example:
<xsl:for-each select="ATOMS/NOBLEGAS">
<tr>
<td><xsl:value-of select="NAME"/></td>
<xsl:choose>
<xsl:when test="MASS < 80">
<td bgcolor="#ccffcc">
<xsl:value-of select="NAME"/></td>
</xsl:when>


<xsl:when test="CHARGE < 100">
<td bgcolor="#ffccff">
<xsl:value-of select="charge"/></td>
</xsl:when>


<xsl:otherwise>
<td><xsl:value-of select="SYMBOL"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>


View the output



3.2.7. <xsl:apply-templates> Element This element applies a template to the element in which it appears, or to its element's child nodes.


Here is an Example:


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


<xsl:template match="/">
<html>


<body>
<h2>The Noble Gases</h2>
<xsl:apply-templates/>
</body>


</html>
</xsl:template>


<xsl:template match="ATOMS">


<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
<br />
</xsl:template>


<xsl:template match="NAME">
Title: <span style="color:#cffcff">
<xsl:value-of select="."/></span>
<br />
</xsl:template>


<xsl:template match="SYMBOL">
Artist: <span style="color:#fccfcc">
<xsl:value-of select="."/></span>
<br />
</xsl:template>


<xsl:template match="CHARGE">
Artist: <span style="color:#fffccc">
<xsl:value-of select="."/></span>
<br />
</xsl:template>


<xsl:template match="MASS">
Artist: <span style="color:#cccfff">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

</xsl:stylesheet>
View the output



4. XSL link:

The link to XSL sheet is done in the XML document, as follows:
<?xml-stylesheet type="text/xsl" href="NobleGases.xsl"?>
  
Google
Web
ScientificSentence
 



chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2011. All rights reserved.