Images

XSL - PART IV


XSL
EXtensible Stylesheet Language(XSL) is an standard from theW3C for describing a style sheet for XML documents. It is the XMLcounterpart to the Cascading Style Sheets (CSS) in HTML and is compatible with CSS2.
XSL is made up of three components: (1) XSL Transformations (XSLT) is the processing language for XSL. It is used to convert XML documents into HTML or other document types and may be used independently of XSL. (2) XML Path Language (XPath) is used to identify and select tagged elements within an XML document, and (3)XSL Formatting Objects (XSL-FO) provides the format vocabulary.
An XSLT is the transformation language, which lets you define a transformation from XMLinto some other format.
For example, you might use XSLT to produce HTML or a different XML structure. You could even use it to produce plain text or to put the information in some other document format.
eXtensible Stylesheet Language Transformation, converts an XML document into another format such as HTMLPDF or text.



Applying Style
<xsl:import>
<xsl:import href="uri" />
The xsl:import element is used to add (import) one stylesheet to another. You can repeat this element in order to import more than one stylesheet. The definitions and template rules of the importing stylesheet are treated as having greater importance that the definitions and template rules of the imported stylesheet.

Using a formal definition, the importing stylesheet is said to have a greater "importprecedence" over the imported stylesheet.
One very important aspect of the xsl:import element is that it allows you to access alibrary of reusable stylesheets to create a desired appearance for the output.
The xsl:import element can only be a child of the xsl:stylesheet or the xsl:transform elements. Further, the xsl:import element must be the first element to occur after the xsl:stylesheet element (for example, before any xsl:include elements). If there is more than one stylesheet being imported, the one that is imported first has a lower import precedence than the one that is imported second, the second has lower import precedence than the third, and so on.
The similar xsl:include element can also be used to add (include) a stylesheet to another, but the definitions and template rules of the included stylesheet are treated equal to the definitions and template rules of the including stylesheet.
The xsl:apply-imports element is used to invoke (apply) any definitions and template rules of the imported stylesheet that have been overridden by the importingstylesheet. A stylesheet should not import itself. This is a self-closing element and it cannot contain any child elements or any content.
href="uri"
The mandatory href attribute is either a relative or absolute URI (Uniform Resource Identifier) address of a valid stylesheet to be imported. A relative URI is resolved against the base URI of the XML source document.
Source code for xslt_examples_import.xsl:
<xsl:stylesheet xmlns:xsl=

"http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:import href="xslt_example_choose.xsl" />

<xsl:template match="/">

<html>

<body>

<xsl:apply-imports />

</body>

</html>

</xsl:template>

</xsl:stylesheet>
Click here to view this XSL file.
<?xml version="1.0" encoding="iso-8859-1"?>

<!-- DWXMLSource="sample23.xml" -->

<!DOCTYPE xsl:stylesheet  [

	<!ENTITY nbsp   " ">

	<!ENTITY copy   "©">

	<!ENTITY reg    "®">

	<!ENTITY trade  "™">

	<!ENTITY mdash  "—">

	<!ENTITY ldquo  "“">

	<!ENTITY rdquo  "”"> 

	<!ENTITY pound  "£">

	<!ENTITY yen    "¥">

	<!ENTITY euro   "€">
]>
<xsl:stylesheet version="1.0" 

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:strip-space elements="phone address" />

<xsl:preserve-space elements="fname" />

<xsl:output method="html" encoding="iso-8859-1" 

doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"

 doctype-system="http://

www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script>

var i=0;

function mm() {

i+=1;

document.data.srno.innerHTML=""+i;

}

</script> 

<meta http-equiv="Content-Type"

 content="text/html; charset=iso-8859-1"/>

<title>Sample XSL Document</title>

<style type="text/css">

<xsl:comment>

.style3 {font-family: Geneva, Arial,

 Helvetica, sans-serif; color: #0033FF; }

</xsl:comment>

</style>

</head>

<body>

<table width="59%" border="1" cellspacing="0" cellpadding="1" id="data">

  <tr>
    <td width="9%" nowrap="nowrap" bgcolor="#6699FF">

<strong>Sr no </strong></td>

    <td width="22%" bgcolor="#6699FF">

<strong>Employee ID </strong></td>

	<td width="30%" bgcolor="#6699FF">

<strong>Name</strong></td>
    
    <td width="20%" bgcolor="#6699FF">

<strong>Department</strong></td>
   
    <td width="19%" bgcolor="#6699FF">

<strong>Designation</strong></td>

	<td width="19%" nowrap="nowrap" bgcolor="#6699FF">Phone No </td>

	<td width="19%" bgcolor="#6699FF">Address </td>

  </tr>

<xsl:for-each select="ebiz/employee_details">

	<tr>

    <td><xsl:number value="position()" format="1. " /></td>

     <td nowrap="nowrap"><span class="style3">

<xsl:value-of select="emp_id" /></span></td>

	 <xsl:choose>

		<xsl:when test="department='TECHICAL[Java]'">

	 		<td nowrap="nowrap" bgcolor="#006699">

<span style="font-weight:bold; color:#FFFFFF">	
<xsl:value-of select="fname"> </xsl:value-of>

<xsl:value-of select="lname"></xsl:value-of></span></td>

	   </xsl:when>

   <xsl:otherwise>

<td nowrap="nowrap"><span class="style3">

<xsl:value-of select="fname"> </xsl:value-of>

<xsl:value-of select="lname"></xsl:value-of></span></td>

</xsl:otherwise>

</xsl:choose>

    <td nowrap="nowrap"><xsl:value-of select="department" /></td>
    
    <td nowrap="nowrap"><xsl:value-of select="designation" /></td>

	<td nowrap="nowrap"> </td>

	<td nowrap="nowrap"> </td>

  </tr></xsl:for-each>

</table>
 
</body>

</html>

</xsl:template>

</xsl:stylesheet>
Click here to view the XML file with formatting.

Applying Style
<xsl:include>
The xsl:include element is used to add (include) a stylesheet to another. You can repeat this element in order to include more than one stylesheet.
The definitions and template rules of the included stylesheet are treated equal to the definitions and template rules of the including stylesheet. The effect is the same as if the included stylesheet was actually part of the stylesheet with which it is being included.
An analogy can be made about this element with regard to the #include used in the C computer language. The similar xsl:import element is also used to add (import) a stylesheet to another, but the definitions and template rules of the importing stylesheet are treated as having greater importance than the definitions and template rules of the imported stylesheet.
The xsl:include element can only occur as a child of the xsl:stylesheet element. All xsl:include elements must occur after all xsl:import elements.

A stylesheet cannot include itself, either directly or indirectly. Nor should a stylesheet be included more than once, either directly or indirectly. Both situations may cause errors due to duplication.
This is a self-closing element and it cannot contain any child elements or any content.href="uri" The mandatory href attribute is either a relative or absolute URI (Uniform Resource Identifier) address of a valid stylesheet to be included.

A relative URI is resolved against the base URI of the XML source document.

Applying Style
<xsl:apply-imports>
<xsl:apply-imports> </xsl:apply-imports>
The xsl:apply-imports element is used to apply (invoke) the definitions and template rules of an imported stylesheet that normally might be overridden by the importing stylesheet.
When one or more stylesheets are imported using the xsl:import element, the imported stylesheets are assigned a lower import precedence than the importing stylesheet. This means that the definitions and rules contained in the imports may be overridden and therefore may not be applied.
The purpose of the xsl:apply-imports element is to permit these overridden rules to be applied when and where you desire them to be applied.
This element has no attributes.
We use the eBIZ.com Staff List XML file for our example with the following header:

<?xml-stylesheet type="text/xsl" href="xslt_example_applyimports.xsl"?>and we name it: xslt_example_applyimports.xml
We import and apply: xslt_example_choose.xsl

(will appear in separate window - use "view source" to see code)
Code for xslt_example_applyimports.xsl:
<xsl:stylesheet xmlns:xsl=

"http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:import href="xslt_example_choose.xsl" />

<xsl:template match="/">

<html>

<body>

<xsl:apply-imports />

</body>

</html>

</xsl:template>

</xsl:stylesheet>
Click here to view this XSL file.
Source code of xslt_imported.xsl:
<?xml version="1.0" encoding="iso-8859-1"?>

<!-- DWXMLSource="sample23.xml" -->

<!DOCTYPE xsl:stylesheet  [

	<!ENTITY nbsp   " ">

	<!ENTITY copy   "©">

	<!ENTITY reg    "®">

	<!ENTITY trade  "™">

	<!ENTITY mdash  "—">

	<!ENTITY ldquo  "“">

	<!ENTITY rdquo  "”"> 

	<!ENTITY pound  "£">

	<!ENTITY yen    "¥">

	<!ENTITY euro   "€">

]>
<xsl:stylesheet version="1.0" xmlns:xsl=

"http://www.w3.org/1999/XSL/Transform">

<xsl:strip-space elements="phone address" />

<xsl:preserve-space elements="fname" />

<xsl:output method="html" encoding="iso-8859-1" 

doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system=

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script>

var i=0;

function mm() {

i+=1;
document.data.srno.innerHTML=""+i;
}

</script> 

<meta http-equiv="Content-Type" 

content="text/html; charset=iso-8859-1"/>

<title>Sample XSL Document</title>

<style type="text/css">

<xsl:comment>

.style3 {font-family: Geneva, Arial, 

Helvetica, sans-serif; color: #0033FF; }

</xsl:comment>

</style>

</head>

<body>

<table width="59%" border="1" cellspacing="0" cellpadding="1" id="data">

  <tr>
    <td width="9%" nowrap="nowrap" bgcolor="#6699FF">

<strong>Sr no </strong></td>

    <td width="22%" bgcolor="#6699FF"><strong>Employee ID </strong></td>

	<td width="30%" bgcolor="#6699FF"><strong>Name</strong></td>
    
    <td width="20%" bgcolor="#6699FF"><strong>Department</strong></td>
   
    <td width="19%" bgcolor="#6699FF"><strong>Designation</strong></td>

	<td width="19%" nowrap="nowrap" bgcolor="#6699FF">Phone No </td>

	<td width="19%" bgcolor="#6699FF">Address </td>
  </tr>
<xsl:for-each select="ebiz/employee_details">
	<tr>

    <td><xsl:number value="position()" format="1. " /></td>

     <td nowrap="nowrap"><span class="style3">

<xsl:value-of select="emp_id" /></span></td>

	 <xsl:choose>

		<xsl:when test="department='TECHICAL[Java]'">

	 		<td nowrap="nowrap" bgcolor="#006699"
><span style="font-weight:bold; color:#FFFFFF">
<xsl:value-of select="fname"> 
</xsl:value-of>  

  <xsl:value-of select="lname">

</xsl:value-of></span></td>
</xsl:when>

   <xsl:otherwise>

<td nowrap="nowrap"><span class="style3"><xsl:value-of select="fname">

 </xsl:value-of>    

<xsl:value-of select="lname"></xsl:value-of></span></td>

</xsl:otherwise>

</xsl:choose>

    <td nowrap="nowrap"><xsl:value-of select="department" /></td>
    
    <td nowrap="nowrap"><xsl:value-of select="designation" /></td>

	<td nowrap="nowrap"><xsl:value-of select="phone" /></td>

	<td nowrap="nowrap"><xsl:value-of select="address" /></td>

  </tr></xsl:for-each>

</table>
 
</body>

</html>

</xsl:template>

</xsl:stylesheet>
Click here to view this XSL file.
Click here to view the XML file

Applying Style
<xsl:attribute-set>
<xsl:attribute-set name="qname" use-attribute-sets="qnames" >

</xsl:attribute-set>
The xsl:attribute-set element defines and names a set containing zero or morexsl:attribute elements. Each of the xsl:attribute elements are applied to the output in the order that they occur inside the xsl:attribute-set element. An xsl:attributeelement allows you to create an attribute node, define a value, and add it to the output.
The xsl:attribute-set element can only be a child of the xsl:stylesheet or the xsl:transform elements.
The concept is that you can create a set of attributes that can be applied more than once by simply calling the attribute set by name.

This is not a self-closing element. The separate closing element is mandatory.
name="qname"
The mandatory name attribute is the name of the attribute set. It must be a qname. A qname is a qualified name that is composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for example,xsl:zipcode or zipcode).
use-attribute-sets="qnames"
The optional use-attribute-sets attribute is a white-space-delimited list of one or more qnames of attribute sets. In other words, it is a collection of attribute set names. Each of the xsl:attribute elements in all of the attribute sets are applied to the output in the order in which they occur in the use-attribute-sets attribute.
This is done by adding the use-attribute-sets attribute to the HTML tag or XSLT element using the following syntax:
<table xsl:use-attribute-sets="name">
<xsl:copy use-attribute-sets="name"> ... </xsl:copy>
We use the eBIZ.com Staff List XML file for our example with the following header:

<?xml-stylesheet type="text/xsl" href="xslt_example_attributeset.xsl"?>and we name it: xslt_example_attributeset.xml
In this example we set the border, cellpadding, and cellspacing values for a table. We only apply the attribute set to one table, but it can be applied to any number of tables to give a similar look.
Code for xsl_attribset.xsl:
<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE xsl:stylesheet[

	<!ENTITY nbsp " ">

 ]>
<xsl:stylesheet version="1.0" xmlns:xsl=

"http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="iso-8859-1" />

<!-- XSL Attribute-set Declaration-->

 <xsl:attribute-set name="tblAttrib">

 <xsl:attribute name="border">
 2
 </xsl:attribute>
 <xsl:attribute name="cellspacing">
 3
 </xsl:attribute>

 <xsl:attribute name="bordercolor">

"#3344dd"
 </xsl:attribute>

 </xsl:attribute-set>

<xsl:template match="/">

<ebizml>

<head>

<title>XSLT xsl:choose Example</title>

</head>

<body>
<span style="width 600px;border: 2px outset blue solidl;display:block">


</span><br /> 

<table xsl:use-attribute-sets="tblAttrib">

<tr>
		<td><b>Name</b></td>

		<td><b>Address</b></td>

		<td><b>Department</b></td>

		<td><b>Designation</b></td>

		<td><b>AGE</b></td>
		
		
	</tr><xsl:for-each select="ebiz/employee_details">

<xsl:sort data-type="number" order="descending" select="age" /> 

 	<tr>
		<td><xsl:value-of select="fname" /> 

<xsl:value-of select="lname" /></td>

		<td><xsl:value-of select="address" /></td>

		<xsl:choose>

			<xsl:when test="department-sub ='Java'">

		<td style="background-color:pink">

<xsl:value-of select="department" />

<xsl:text>[</xsl:text><xsl:value-of select="department-sub" />

<xsl:text>]</xsl:text></td>
</xsl:when>

		<xsl:when test="department ='TECHNICAL'">

		<td style="background-color:#
FFCC99;color:white;font-weight:bold">
			
			<xsl:value-of select="department" />
			 
			<xsl:text>[</xsl:text><xsl:value-o

f select="department-sub" /><xsl:text>]</xsl:text></td>

		</xsl:when>

		<xsl:otherwise>

			<td><xsl:value-of select="department" /> </td>

		</xsl:otherwise>

		</xsl:choose>

		<xsl:choose>
			
			<xsl:when test="department='TECHNICAL'">

		<td style="background-color:#
FFCC99;color:white;font-weight:bold">

		 	<xsl:value-of select="designation" />

		</td>

		</xsl:when>

	<xsl:otherwise>
			 
		<td style="font-weight:bold">

			<xsl:value-of select="designation" />

		</td>

		</xsl:otherwise>

	</xsl:choose>

	<td>
		<xsl:value-of select="age" />

	</td>

	</tr>

 </xsl:for-each>

</table>

</body>

</ebizml>

</xsl:template>

</xsl:stylesheet>
View the XSL file or XML file formatted with this XSL.

Applying Style
<xsl:comment>
<xsl:comment> …... </xsl:comment>
The xsl:comment element is used to generate a comment in the output.
The purpose of a comment is simply to provide information that may be of interest or importance to human users of the code.
For example, you could list information about the program itself, such as the date of creation and programmer names or catalog changes and corrections. Carefully placed comments can also be used to debug code.
In one regard, the ability to comment in XSL is far more sophisticated than the ability to comment in other languages such as HTML and VBScript.
Like other elements in XSL, the xsl:comment element can make use of extension functions and can contain functions. While this may be of limited use for a comment, you can do things like include date-time stamps in your comment as demonstrated in this code fragment:
<xsl:comment> <xsl:value-of select="Date:toString()" /> </xsl:comment>
This element has no attributes. It is not a self-closing tag. The separate closing element is mandatory.
We use the eBIZ.com Staff List XML file for our example with the following header:

<?xml-stylesheet type="text/xsl" href="xslt_example_comment.xsl"?> 

and we name it: xslt_example_comment.xml

Applying Style
<xsl:processing-instruction>
<xsl:processing-instruction name="process-name" > </xsl:processing-instruction>
The xsl:processing-instruction element writes (generates) a processing instruction to the output. The content of this element becomes the text of the processing instruction. The syntax is:
<xsl:processing-instruction name="pi_name">

processing instruction text goes here ...

</xsl:processing-instruction>
This text should not contain any character references. In other words, all characters must be directly represented by the selected character encoding (i.e., an < cannot be represented by an <).
This is not a self-closing element. The separate closing element is mandatory.
name="process-name"
The mandatory name attribute is the name of the processing instruction. The name cannot contain a colon (:).
Processing instructions are rarely used in XML. However, the W3C standard permits the application of a CSS (Cascading Style Sheet) file to an XML document. This is accomplished with an <?xml_stylesheet ... ?> processing instruction which is directly compariable to the HTML <link type="text/css" href="style.css> tag.
This code fragment demonstrates how to do this:
<xsl:processing-instruction name="xml-stylesheet">

type="text/xsl" href="style_rules.css" title="Large Print"

</xsl:processing-instruction>
This creates the following tag:
<?xml-stylesheet href="style_rules.css" type="text/css" title="Large Print"?>

0 comments: