Back
Next
XSLT
that we looked at were simplified style sheets.
Simplified style sheets are good as a starting point when you're creating an
XSLT Template, and they can be all you
need in some cases. However, to utilize the more sophisticated functionality of
Extensible Stylesheet Language
Template, you need to use full style sheets.
Here, we will take the simplified style sheet. We will also introduce you to templates as a way of breaking up your code and look
in a bit more detail at how XSLT processors construct a result from some source XML. You'll learn
soon here.
XSLT Template Style sheet Structure
The simplified style sheets that we will use here it will make a good starting point when we're creating an XSLT style sheet. Simplified
Extensible Stylesheet Language aren't all that common in larger applications because they're
fairly restricted in what they can do, especially with document-oriented XML.
The equivalent full style sheet for the simplified style sheet looks very similar.
The content of the simplified style sheet is wrapped in two elements – <xsl:template> and
<xsl:stylesheet> – to create any out put The <xsl:stylesheet> element takes
the version attribute and the XSLT namespace
declaration instead of the < html> element, giving the following:
Complete Example of first XSLT
XML Code for XSLT Attributes
<?xml version="1.0" encoding="ISO-8859-15"?>
<?xml-stylesheet type="text/xsl" href="employees.xsl"?>
<globalGuideLine>
<employee>
<FirstName>Angela</FirstName>
<LastName>Julie</LastName>
<gender>f</gender>
</employee>
<employee>
<FirstName>Alfa</FirstName>
<LastName>Cute</LastName>
<gender>m</gender>
</employee>
</globalGuideLine>
XSLT Code for XML transformation [employees.xsl]
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xsl:version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>XSLT Temples at Global Guide Line XSLT Tutorial</title>
</head>
<body style="font-family:Arial,helvetica,sans-serif;font-size:12pt;
background-color:#EEEEEE">
<xsl:for-each select="globalGuideLine/employee">
<div style="background-color: #cccccc;color:000000;padding:0px">
<span style="font-weight:bold;color:#000000">
<xsl:value-of select="FirstName"/> <xsl:value-of select="LastName"/></span>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<span style="font-style:none">
<xsl:if test="gender='m'">
<font color="red"><b>Mr. </b></font> <xsl:value-of
select="FirstName"/> is working at Global Guide Line
</xsl:if>
<xsl:if test="gender='f'">
<font color="red"><b>Miss </b></font><xsl:value-of
select="FirstName"/> is working at Global Guide Line
</xsl:if>
</span>
</div>
</xsl:for-each>
</body>
</html>
Out put of XSLT Template Example
AngelaJulie
Miss Angela is working at Global Guide Line
AlfaCute
Mr. Alfa is working at Global Guide Line
Complete Example of XSLT Templates
View complete sources and outputs by simply clicking on file name of XSLT, XML, Out Put XSLT Template
Style sheet Document Elements
The document element of a full style sheet is <xsl:stylesheet> a <stylesheet> element in the namespace
https://www.w3.org/1999/XSL/Transform – as usual we are using the prefix xsl here but you could use whatever you
liked as long as it's associated with the
Extensible Stylesheet Language namespace with a namespace declaration. Like the document
element in simplified style sheets, the <xsl:stylesheet> element needs to declare the XSLT namespace and give
the version of XSLT that's used in the style sheet with a version attribute. This time, though, the version
attribute doesn't need to be qualified with the xsl prefix because it already lives on an element in the XSLT
namespace, so the processor knows it's part of XSLT.
You can also use <xsl:transform> as the document element in a full stylesheet, rather than
<xsl:stylesheet>. There is no difference in functionality between the two document XSLT elements – they each use exactly the same
attributes and do exactly the same thing as output. Some people prefer to use <xsl:transform> when doing transformations
that aren't producing
presentation-oriented formats such as XSL-FO or HTML. Personally, We use <xsl:transform> all the time.
Back
Next
|